Last active
January 30, 2024 16:20
-
-
Save philfreo/44e2e26a65820497db234d0c66ed58ae to your computer and use it in GitHub Desktop.
Three ways to make a PDF from HTML in Python (preferred is weasyprint or phantomjs)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def render_pdf_weasyprint(html): | |
from weasyprint import HTML | |
pdf = HTML(string=html.encode('utf-8')) | |
return pdf.write_pdf() | |
def render_pdf_xhtml2pdf(html): | |
"""mimerender helper to render a PDF from HTML using xhtml2pdf. | |
Usage: http://philfreo.com/blog/render-a-pdf-from-html-using-xhtml2pdf-and-mimerender-in-flask/ | |
""" | |
from xhtml2pdf import pisa | |
from cStringIO import StringIO | |
pdf = StringIO() | |
pisa.CreatePDF(StringIO(html.encode('utf-8')), pdf) | |
resp = pdf.getvalue() | |
pdf.close() | |
return resp | |
def render_pdf_phantomjs(html): | |
"""mimerender helper to render a PDF from HTML using phantomjs.""" | |
# The 'makepdf.js' PhantomJS program takes HTML via stdin and returns PDF binary via stdout | |
# https://gist.github.com/philfreo/5854629 | |
# Another approach would be to have PhantomJS do a localhost read of the URL, rather than passing html around. | |
from subprocess import Popen, PIPE, STDOUT | |
import os | |
p = Popen(['phantomjs', '%s/pdf.js' % os.path.dirname(os.path.realpath(__file__))], stdout=PIPE, stdin=PIPE, stderr=STDOUT) | |
return p.communicate(input=html.encode('utf-8'))[0] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PhantomJS: generate PDF to stdout from HTML in stdin. | |
// Example: echo "<b>test</b>" | phantomjs makepdf.js > test.pdf && open test.pdf | |
var page = require('webpage').create(), | |
fs = require('fs'); | |
page.viewportSize = { width: 600, height: 600 }; | |
page.paperSize = { format: 'Letter', orientation: 'portrait', margin: '1cm' }; | |
page.content = fs.read('/dev/stdin'); | |
window.setTimeout(function() { | |
page.render('/dev/stdout', { format: 'pdf' }); | |
phantom.exit(); | |
}, 1); |
And in python3? I'm facing this problem -> https://stackoverflow.com/questions/68284524/how-encode-this-string-to-export-it-to-pdf
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would use a headless browser. Of these options, phantomjs, but I would look into more modern headless browser options since this gist is a few years old.