Skip to content

Instantly share code, notes, and snippets.

@molszewski
Last active August 29, 2015 13:55
Show Gist options
  • Save molszewski/8689640 to your computer and use it in GitHub Desktop.
Save molszewski/8689640 to your computer and use it in GitHub Desktop.
WeasyPrint + Jinja2 spike
#!venv/bin/python
# Script requires:
# - virtualenv in venv dir (virtualenv venv)
# - XQuartz-2.7.5 (MacOSX only, installed separately from https://xquartz.macosforge.org/landing/)
# - cairo pango gdk-pixbuf libxmllibxslt libffi (brew install cairo pango gdk-pixbuf libxmllibxslt libffi)
# - WeasyPrint (pip install WeasyPrint)
# - Jinja2 (pip install Jinja2)
from weasyprint import HTML
from jinja2 import Environment, FileSystemLoader
# load template
env = Environment(loader=FileSystemLoader('.'))
template = env.get_template('template.html')
# render template
rendered = template.render(messages=['Hello','World'])
# write to PDF
HTML(string=rendered).write_pdf('weasyprint_jinja2_spike_output.pdf')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment