Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert html to pdf using Python/Flask

I want to generate pdf file from html using Python + Flask. To do this, I use xhtml2pdf. Here is my code:

def main():
    pdf = StringIO()
    pdf = create_pdf(render_template('cvTemplate.html', user=user))
    pdf_out = pdf.getvalue()
    response = make_response(pdf_out)
    return response

def create_pdf(pdf_data):
    pdf = StringIO()
    pisa.CreatePDF(StringIO(pdf_data.encode('utf-8')), pdf)
    return pdf

In this code file is generating on the fly. BUT! xhtml2pdf doesn't support many styles in CSS, because of this big problem to mark page correctly. I found another instrument(wkhtmltopdf). But when I wrote something like:

pdf = StringIO()
data = render_template('cvTemplate1.html', user=user)
WKhtmlToPdf(data.encode('utf-8'), pdf)
return pdf

Was raised error:

AttributeError: 'cStringIO.StringO' object has no attribute 'rfind'

And my question is how to convert html to pdf using wkhtmltopdf (with generating file on the fly) in Flask?

Thanks in advance for your answers.

like image 762
Dmitry_Mahrachev Avatar asked Jan 27 '15 08:01

Dmitry_Mahrachev


4 Answers

The page need render, You can use pdfkit:

https://pypi.python.org/pypi/pdfkit

https://github.com/JazzCore/python-pdfkit

Example in document.

import pdfkit

pdfkit.from_url('http://google.com', 'out.pdf')
pdfkit.from_file('test.html', 'out.pdf')
pdfkit.from_string('Hello!', 'out.pdf')  # Is your requirement?
like image 118
uwu Avatar answered Oct 22 '22 18:10

uwu


Have you tried with Flask-WeasyPrint, which uses WeasyPrint? There are good examples in their web sites so I don't replicate them here.

like image 34
chfw Avatar answered Oct 22 '22 16:10

chfw


Conversion in 3 Steps from Webpage/HTML to PDF

Step1: Download library pdfkit

$ pip install pdfkit

Step2: Download wkhtmltopdf

For Ubuntu/Debian:

sudo apt-get install wkhtmltopdf

For Windows:

(a)Download link: WKHTMLTOPDF

(b)Set: PATH variable set binary folder in Environment variables.

Step3: Code in Python to Download:

(i) Already Saved HTML page

import pdfkit
pdfkit.from_file('test.html', 'out.pdf')

(ii) Convert by website URL

import pdfkit
pdfkit.from_url('https://www.google.co.in/','shaurya.pdf')

(iii) Store text in PDF

import pdfkit
pdfkit.from_string('Shaurya Stackoverflow','SOF.pdf')
like image 41
Shaurya Uppal Avatar answered Oct 22 '22 16:10

Shaurya Uppal


Not sure if this would assist anyone but my issue was capturing Bootstrap5 elements as a pdf. pdfkit did not do so and heres a work around on windows using html2image and PIL. This is limited and does not take a full page screenshot.

from html2image import Html2Image
from PIL import Image

try:
   hti.screenshot(html_file=C:\yourfilepath\file.html, save_as="test.png")

finally:
   image1 = Image.open(r'C:\yourfilepath\test.png')
   im1 = image1.convert('RGB')
   im1.save(r'C:\yourfilepath\newpdf.pdf')
like image 22
idcabnun Avatar answered Oct 22 '22 17:10

idcabnun