Python HTML to PDF with full support for CSS3 and HTML5
Asked Answered
B

2

16

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.

Bedivere answered 25/12, 2014 at 18:8 Comment(6)
Were you able to find some solution?Saleh
It is 2017 now , does the libs you used be 100% ?Mcmath
Hey, It is 2019 now, did you find any python library, that supports full css3 and html5 .Igal
@Ales Malticic do think my answer will help other with your question?Jiminez
Its 2021, any lucky with a good libVillalobos
Its 2024, whats the best bet to convert an HTML file to PDF ?Archivist
J
7

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

Jiminez answered 23/11, 2018 at 15:49 Comment(0)
A
2

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.

Audrit answered 28/5, 2015 at 1:50 Comment(3)
I you read my question is that I already have a solution which works 97% for css3 properties, but I have found some other solution that works 100% for css3 (unfortunately not for python) and as far as I know there is no python lib that would accomplish that.Bedivere
Your question was if there is something for python. And yes there is something in python which also include html5 and css3 when porting html to pdf.Audrit
please read my question again carefully. I said that I already use a Python solution that supports html5 and css3(wkhtmltopdf) but that support is not 100%. There are tons of questions on here for simple conversion. All your solutions either use the same lib or have the same flaws, and none of them can 100% transform a css3 based html page to a pdf without noticeable differences.Bedivere

© 2022 - 2024 — McMap. All rights reserved.