Here is my experience after following the hints on this page.
pyPDF can't embed images into files. It can only split and merge. (Source: Ctrl+F through its documentation page) Which is great, but not if you have images that are not already embedded in a PDF.
pyPDF2 doesn't seem to have any extra documentation on top of pyPDF.
ReportLab is very extensive. (Userguide) However, with a bit of Ctrl+F and grepping through its source, I got this:
- First, download the Windows installer and source
Then try this on Python command line:
from reportlab.pdfgen import canvas from reportlab.lib.units import inch, cm c = canvas.Canvas('ex.pdf') c.drawImage('ar.jpg', 0, 0, 10*cm, 10*cm) c.showPage() c.save()
All I needed is to get a bunch of images into a PDF, so that I can check how they look and print them. The above is sufficient to achieve that goal.
ReportLab is great, but would benefit from including helloworlds like the above prominently in its documentation.
Answer from Evgeni Sergeev on Stack OverflowBest library for creating graphic PDF documents?
How to create PDF files in Python - Stack Overflow
PDF generator, where should I start?
Anyway to generate pdf with specific format using Python?
How do I generate a PDF report from HTML in Python?
Use WeasyPrint for print-oriented HTML and CSS, or Playwright with Chromium when the report needs JavaScript. Nutrient API provides hosted HTML-to-PDF conversion with custom fonts, headers, footers, and page numbers.
Does Python have built-in PDF generation?
No. Python’s standard library doesn’t include a PDF generator. Use a third-party library such as fpdf2 for basic documents, ReportLab for reports, or WeasyPrint for HTML templates.
Is the Python to PDF tool free to use?
I have an application for which I need to auto-generate some diagrams as PDF files. The graphics aren't anything particularly fancy, just line drawings and some text.
My first instinct was to generate LaTeX code in Python to draw the graphics with TikZ, but I feel like there's probably a better way without the middleman. I see there are a variety of different libraries for generating PDFs, so I'm looking for someone who has used one or more of them to maybe point me towards one which would suit my needs the best.
Edit: I should mention that I currently am manually creating the diagrams in LaTeX with TikZ. It works "well" (speaking as someone fluent in LaTeX, I doubt anyone who isn't would think this is a good solution at all), but it feels weird to add an extra step of generating code that generates the files instead of generating the files I need directly. But TikZ is a good example of the type of control I need - these diagrams aren't super fancy, just showing and labeling arrangements of chairs in rooms.
Here is my experience after following the hints on this page.
pyPDF can't embed images into files. It can only split and merge. (Source: Ctrl+F through its documentation page) Which is great, but not if you have images that are not already embedded in a PDF.
pyPDF2 doesn't seem to have any extra documentation on top of pyPDF.
ReportLab is very extensive. (Userguide) However, with a bit of Ctrl+F and grepping through its source, I got this:
- First, download the Windows installer and source
Then try this on Python command line:
from reportlab.pdfgen import canvas from reportlab.lib.units import inch, cm c = canvas.Canvas('ex.pdf') c.drawImage('ar.jpg', 0, 0, 10*cm, 10*cm) c.showPage() c.save()
All I needed is to get a bunch of images into a PDF, so that I can check how they look and print them. The above is sufficient to achieve that goal.
ReportLab is great, but would benefit from including helloworlds like the above prominently in its documentation.
I suggest Pdfkit. (installation guide)
It creates pdf from html files. I chose it to create pdf in 2 steps from my Python Pyramid stack:
- Rendering server-side with mako templates with the style and markup you want for you pdf document
- Executing
pdfkit.from_string(...)method by passing the rendered html as parameter
This way you get a pdf document with styling and images supported.
You can install it as follows :
using pip
pip install pdfkit- You will also need to install wkhtmltopdf (on Ubuntu).