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

47,046

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/

Share:
47,046
Catalin D Voinescu
Author by

Catalin D Voinescu

Updated on November 05, 2020

Comments

  • Catalin D Voinescu
    Catalin D Voinescu over 3 years

    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.