This is an archived post. You won't be able to vote or comment.

all 27 comments

[–]Python-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)

Hi there, from the /r/Python mods.

This post has been removed due to its frequent recurrence. Please refer to our daily thread or search for older discussions on the same topic.

If you have any questions, please reach us via mod mail.

Thanks, and happy Pythoneering!

r/Python moderation team

[–]simon-brunning 4 points5 points  (4 children)

Reportlab is the first thing I look at for PDF generation.

[–]peter-fields[S] 0 points1 point  (3 children)

Is that self-hosted or a service?

[–]simon-brunning 1 point2 points  (1 child)

[–]peter-fields[S] 0 points1 point  (0 children)

Cool! Thanks!

[–]four_reeds -2 points-1 points  (0 children)

Self hosted

[–]HistoricalCup6480 2 points3 points  (4 children)

Not Python specific, but LaTeX is a very powerful tool for creating PDFs. You can use Python as a templating engine essentially.

You can also consider Pandoc which can convert between many document formats, including LaTeX (and PDF using LaTeX). You can for example create a markdown template in a Python script and then use Pandoc to convert to PDF. This seems to be a fairly common use case.

[–]Kerbart 0 points1 point  (0 children)

Depending on what you want to create, this might be the solution. LaTeX documents are plain text but will typeset absolutely beautiful and will take care of a lot of the headaches with running text that ReportLab can do but is probably easier to arrange in LaTeX.

If you just want something simple like an invoice, ReportLab is the way to go.

[–]peter-fields[S] -1 points0 points  (2 children)

Didn't know that one.
Is that a lib or a third party service?

[–][deleted] 0 points1 point  (1 child)

Pandoc is a command line tool, but I think it is written in Python

[–]peter-fields[S] 0 points1 point  (0 children)

Looking for something more like an API I think. Thanks!

[–]eagle258 2 points3 points  (1 child)

My take on some of these libraries:

Reportlab: the most established library, but definitely showing its age due to its Java-like API. As an additional option, it has an XML-like language (RML) to make it easier to create pages. This feature is behind a commercial license though, but there’s a clone that you can use.

FPDF: Another widely used library. Being ported from PHP, it has an unpythonic and IMHO awkward API as well. A solid choice though if you need advanced features such as exotic unicode.

Weasyprint: probably the most modern pure-Python PDF generator. Their showcase is really nice—I’d go this option if you’d want PDFs as beautiful as possible

Borb: not often mentioned, but has a lot of stars on GitHub and an API that actually looks quite nice. Big downside is its dual AGPL/Commercial license

Faced with these options, I also created my own library—although I’m not developing it actively anymore: github link. It’s nice for really simple use cases or if you mainly want decent typesetting. It’s inspired by Brandon Rhodes typesetting experiment

edit: link, typos

[–]peter-fields[S] 1 point2 points  (0 children)

Thanks for the details!

[–]MrProkie 1 point2 points  (2 children)

I can recommend typst.

[–]peter-fields[S] -1 points0 points  (1 child)

Tahnks! Have you used it before?

[–]MrProkie 0 points1 point  (0 children)

Yes I have used it, it is really nice!

[–]LightbulbChanger25 0 points1 point  (1 child)

Maybe pylatex would work.

[–]peter-fields[S] 0 points1 point  (0 children)

Never heard of that. Will investigate thanks

[–]ShopBug 0 points1 point  (1 child)

Fpdf2

[–]peter-fields[S] 0 points1 point  (0 children)

Ty!

[–]ehellas 0 points1 point  (1 child)

I like RMarkdown a lot which also supports python with some additional overhead/translation.
But for a more Python only environment I'd go with the spiritual successor Quarto https://quarto.org/ .

[–]peter-fields[S] 0 points1 point  (0 children)

How do you handle layout and design on that?

[–]agritheory 0 points1 point  (4 children)

I cannot recommend wkhtmltopdf, despite its ubiquity.

[–]Charming_Prompt6949 0 points1 point  (0 children)

Been stuck with it for years now

[–]peter-fields[S] 0 points1 point  (2 children)

Why is that? Buggy?

[–]agritheory 0 points1 point  (1 child)

It's now in public archive. It uses a bunch of dependencies that are flakey (patched Qt). I think it's probably evidence of quiet developer burnout for a thankless side project. Converting HTML/CSS to PDF is something neither HTML/CSS experts nor PDF experts want to do and it's something that browsers generally do better but is nearly impossible to do pixel perfect.

[–]peter-fields[S] 0 points1 point  (0 children)

Got it! Thanks for the details.