Skip to content

Instantly share code, notes, and snippets.

@whitelynx
Last active October 11, 2017 18:02
Show Gist options
  • Save whitelynx/88ee979ee2efd03f120a3e609f33bc95 to your computer and use it in GitHub Desktop.
Save whitelynx/88ee979ee2efd03f120a3e609f33bc95 to your computer and use it in GitHub Desktop.
''' WeasyPrint JPG test case
Testing:
# virtualenv virtenv
# virtenv/bin/pip install WeasyPrint
# virtenv/bin/python test-jpg.py
'''
import logging
import weasyprint
from weasyprint import CSS, HTML
from weasyprint.fonts import FontConfiguration
logging.basicConfig(level=logging.NOTSET)
print('cairo version: {}'.format(weasyprint.pdf.cairo.cairo_version_string()))
print('pango version: {}'.format(weasyprint.text.pango.pango_version()))
css = '''
html {
background: url(https://upload.wikimedia.org/wikipedia/commons/0/0e/Felis_silvestris_silvestris.jpg);
}
img {
border: 10px solid black;
width: 200px;
}
'''
html = '''
<html>
<head>
<title>test</title>
</head>
<body>
<h1>This is a test.</h1>
<p>This is only a test.</p>
<img src="https://upload.wikimedia.org/wikipedia/commons/0/0e/Felis_silvestris_silvestris.jpg">
</body>
</html>
'''
with open('jpg-test.pdf', 'wb') as outFile:
HTML(string=html).write_pdf(outFile, stylesheets=(CSS(string=css),))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment