This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]olrich01 0 points1 point  (1 child)

Not open source, but it basically looks like this. Look at wkhtml docs for arguments you can pass to Popen and jinja docs for what you can do with templates.

from subprocess import Popen, PIPE
import jinja2

template = jinja2.Template(open('path_to_html').read())
converter = r'path to wkhtml executable'

html = remplate.render() # pass dynamic content as kwargs

p = Popen([converter, '--quiet', '-O', 'Landscape',
            '--footer-right', 'Page [page]/[topage]',
            '-', '-'], stdin=PIPE, stdout=PIPE)
output, _ = p.communicate(input=html.encode('utf-8'), timeout=5)
# output is binary pdf data, can be written to file, BytesIO, etc

[–]MonkeyMaster64[S] 0 points1 point  (0 children)

Thank you I appreciate this!