Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python HTML to PDF with full support for CSS3 and HTML5

I am working on a Python/Django project, where I have to convert some of my highly styled html content to PDF. Currently I am using the wkhtmltopdf library. While the conversion works almost perfectly there are some issues regarding CSS3 and HTML5 support. Some of the styles don't convert correctly to pdf and I have to alter the CSS and HTML to make it more print friendly so I can convert to PDF correctly. Until lately I thought there is no tool that could make 100% conversion but than I found this with an online demo http://www.html-to-pdf.net/free-online-pdf-converter.aspx which has a far superior HTML5 and CSS3 support, but it is written for .NET. Is there anything with this level of conversion with Python support?

Note: I will have lots of HTML pages that will need conversion and tweaking the CSS for each one is not a good option for me. I would need something that could make a 100% conversion regarding the styling from HTML to PDF.

like image 528
Ales Maticic Avatar asked Dec 25 '14 18:12

Ales Maticic


People also ask

Can I create a PDF with Python?

FPDF is a Python class that allows generating PDF files with Python code. It is free to use and it does not require any API keys. FPDF stands for Free PDF.


2 Answers

If you or someone else is still looking for some solutions, check out this: Render HTML to PDF in Django site

also https://github.com/chrisglass/xhtml2pdf or https://github.com/JazzCore/python-pdfkit

I hope this will help most of you.

like image 96
Tom-Oliver Heidel Avatar answered Oct 23 '22 04:10

Tom-Oliver Heidel


You should check weasyprint, It supports most of the CSS3 and HTML5, at the moment it doesn't support flex-wrap: wrap propierties as far as I know.

I used xhtml2pdf with Reportlab it supports some CSS3 (supports HTML5 CSS 2.1). I don't recommend it when you have a better and simple tool like weasyprint.

Simple Django Tutorial with weasyprint

like image 37
Gonzalo Garcia Avatar answered Oct 23 '22 04:10

Gonzalo Garcia