-
-
Save bbinet/97a92927cb310eec7b1d0bc7e5f607fc 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment