12

I have for some time looking for something to be able to create PDFs and integrate with my current project in Python 3. The usual references are Reportlab or pyPDF. But these two are not yet compatible with Python 3. I do see that since 2010 there has been some discussion about the port in Reportlab, but as of today I can't find any reference to the port.

Another alternative is to use an intermediate format from Python 3 to be consumed in another program, wrapped with a subprocess call. Relatorio does this. I might consider doing something similar or even generate XHTML/CSS and use xhtml2pdf.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
cyraxjoe
  • 5,661
  • 3
  • 28
  • 42
  • Reportlab is being ported now: https://bitbucket.org/rptlab/reportlab/branch/py33 – Matthias Kestenholz Dec 31 '13 at 11:47
  • 2
    Reportlab 3 (released [February 2013](http://www.reportlab.com/software/documentation/relnotes/30/)) now supports Python 3.3+ – Alasdair Feb 21 '14 at 10:15
  • Actually both Reportlab and its "nicer" interface django-easy-pdf now work on Python 3 (now that xhtml2pdf has beta Python 3 support) – I explained how to get things to work [here](http://stackoverflow.com/a/43097663/544059). – metakermit Mar 29 '17 at 15:34
  • for people still looking for an alternative to Reportlab and fpdf, check [pdfme](https://github.com/aFelipeSP/pdfme). It's the most powerful library in python to create PDF documents. – Felipe Sierra Jul 15 '21 at 02:28

7 Answers7

5

There is a new kid on the block which look promising. It supports print css features like page break.

Try weasyprint

yvess
  • 1,992
  • 19
  • 17
  • OP is talking about specifically using a PDF processor within Python 3, not a stand-alone service. Edit: never mind I see that weasyprint is on github and can be scripted. Here is a more relevant link: https://github.com/Kozea/WeasyPrint – james-see Nov 23 '16 at 14:37
4

The cairo library with the pycairo (not py2cairo, that's for python 2) binding works with python 3.x and can produce PDF output (among others).

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • 1
    Following your suggestion I start looking for some library around cairo/pango and I found this https://savannah.nongnu.org/projects/pypdflib which it has some nice abstractions of documents. It doesn't officially support python3, but based on the support for python3 on cairo and pango it seems feasible with a few modifications. – cyraxjoe Aug 18 '12 at 22:08
  • 1
    Yeah, cairo itself is good for drawing but only passable for creating text. Pypdflib sounds good. An alternative might be to use pango (http://www.pango.org/) with cairo for text. If you install pygtk, it has a pangocairo module. But except from some examples, there isn't much documentation. – Roland Smith Aug 19 '12 at 06:48
  • Just to inform to the future adventures in python 3 and pdf's I end up porting a python2 library to python 3 https://bitbucket.org/cyraxjoe/py3fpdf. – cyraxjoe Aug 25 '12 at 18:33
  • 1
    @cyraxjoe Your bitbucket link is 404-ing. – Roland Smith Jul 29 '19 at 14:00
  • thanks for the heads up, is now hosted on github https://github.com/cyraxjoe/pypfop – cyraxjoe Jul 29 '19 at 18:21
3

In the end I find it difficult to work with pycario/pango, pycairo stil misses some method for png's and PIL is not quite yet supported in python3, also carries a lot of dependencies (the idea of using pycario) so I end up porting a python 2 library to python 3, is not very advanced but gets the jobs done for some basic pdfs, it's called py3fpdf

--- update --

It turn out that p3fpdf didn't get the jobs done n_n'. So to tell the end of the story, I made a preprocessor to apache fop it s called pypfop. I just recently decouple with the full project, It a work in progress specially with documentation, the examples may be illustrative.

cyraxjoe
  • 5,661
  • 3
  • 28
  • 42
2

You could got the HTML/CSS route and use prince although it's not free software.

If your source is not too complex, you can also try a pure python solution as in http://code.activestate.com/recipes/189858-python-text-to-pdf-converter/

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
0

You could shell out and call text2pdf

the wolf
  • 34,510
  • 13
  • 53
  • 71
  • 2
    The question already suggests using xhtml2pdf as an alternative, so 'do the same with text2pdf' doesn't really answer the question. – Reinout van Rees Nov 14 '12 at 21:35
0

Port for 2.7 and 3.3 is currently a work in progress:

https://github.com/nakagami/reportlab

PIL has not yet been ported to Python3, so ReportLab will not work completely.

Jeff Bauer
  • 13,890
  • 9
  • 51
  • 73
nakagami
  • 31
  • 2
0

Pillow is a pretty good port of PIL to Python 3, and it's now available in Debian experimental and Ubuntu 13.04. That should unblock a Python 3 port of ReportLab, which I would love to see. @nakagami: your github report seems to fail for me with python3 setup.py build but it could be shallow. How official is your port? Have you contacted the RL folks to see if they're interested in your work? I don't think their code is available in a public vcs.

Barry
  • 1
  • 1