Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a PDF file, including images and text, from Python? [closed]

Tags:

I am looking for a way to create a sheet of labels, as a PDF file, from a Python program. Each label has one or two images, and a few lines of text (same font, e.g. Helvetica or Arial, but possibly different sizes, and using bold and italic). These being labels, it is important that the elements are positioned correctly on the page. Some of the labels are addresses, so the text can vary and have different line lengths and number of lines.

I would like to be able to tell when text did not fit in the space available for it, so that the program could try a few strategies automatically (e.g. change font to Arial Narrow, reduce font size) before bailing out. I definitely don't want the text to overrun or wrap without notice.

I have looked at pypdf, but it doesn't seem to be able to do what I need (or maybe I'm missing something). I don't mind creating a document in a different format using a library that supports the features I need, as long as it can be converted to PDF programmatically. As a last resort, I can also use a library that can create a raster image, because that can be converted trivially to PDF, but I prefer a PDF that contains native text, not just a bitmap of it.

like image 969
Catalin D Voinescu Avatar asked Nov 04 '13 13:11

Catalin D Voinescu


People also ask

How do you close a PDF in Python?

open() to open files and f. close() to close it.


1 Answers

Take a look at reportlab.

Here is the userguide: https://www.reportlab.com/docs/reportlab-userguide.pdf

Here is a nice Tutorail: http://www.blog.pythonlibrary.org/2010/03/08/a-simple-step-by-step-reportlab-tutorial/

like image 192
T. Christiansen Avatar answered Sep 18 '22 14:09

T. Christiansen