Search code examples
pythonpdffpdfpypdf

Place a vertical or rotated text in a PDF with Python


I'm currently generating a PDF with PyFPDF. I also need to add a vertical/rotated text. Unfortunately, it's not directly supported in PyPDF2 as far as I see. There are solutions for FPDF for PHP.

Is there a way to insert vertical or rotated text in a PDF from Python, either with PyFPDF or with another library?


Solution

  • I believe you can do so with PyMuPDF. I've inserted text with the module before but not rotated text. There is a rotate parameter in the insertText method so hopefully it will work for you.

    It can be done as follows:

    import fitz
    doc = fitz.open(filename)
    page = doc[0]
    point = fitz.Point(x, y) # in PDF units (1 / 72 of an inch)
    page.insertText(
      point,
      text="Hello World",
      fontsize=8,
      fontname="Helvetica", # Use a PDF Base 14 Fonts, else check documentation
      color=(0, 0, 0),
      rotate=90
    )
    doc.save(filename, incremental=True)
    doc.close()