Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 274
Michael Avatar asked Nov 07 '22 07:11

Michael


1 Answers

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()
like image 153
J. Owens Avatar answered Nov 14 '22 20:11

J. Owens