Created
December 16, 2018 21:06
-
-
Save pangyuteng/2af5d4ee692f05fab20e72630260e2be to your computer and use it in GitHub Desktop.
report (image,text) generation in python using jinja2,html,wkhtmltopdf via imgkit
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
# sudo apt-get install wkhtmltopdf | |
# pip install imgkit | |
# pip install jinja2 | |
import subprocess | |
#subprocess.call(['wget','https://image.shutterstock.com/display_pic_with_logo/136306/722718082/stock-photo-healthy-food-clean-eating-selection-fruit-vegetable-seeds-superfood-cereals-leaf-vegetable-on-722718082.jpg']) | |
#subprocess.call(['wget','https://www.onlygfx.com/wp-content/uploads/2017/11/grunge-circle-frame-1-1024x982.png']) | |
content = dict( | |
background_img_path = "stock-photo-healthy-food-clean-eating-selection-fruit-vegetable-seeds-superfood-cereals-leaf-vegetable-on-722718082.jpg", | |
foreground_img_path = "grunge-circle-frame-1-1024x982.png", | |
logo_text = "FOO", | |
) | |
from jinja2 import Environment | |
import imgkit | |
HTML = """ | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<style type="text/css"> | |
.container { position:relative; } | |
.background { position:absolute; top: 0px; left: 0px; z-index: 1; } | |
.frame { position:absolute; top: 100px; left: 100px; width: 200px; z-index: 3;} | |
.circle { | |
position: absolute; top: 103px; left: 110px; width: 180px; height: 185px; border-radius: 50%; | |
font-size: 50px; color: #000; line-height: 200px; text-align: center; background: #fff; z-index: 2;} | |
} | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<img class="background" src={{background_img_path}}> | |
<img class="frame" src={{foreground_img_path}}> | |
<div class="circle">{{logo_text}}</div> | |
</div> | |
</body> | |
</html> | |
""" | |
rendered_output = Environment().from_string(HTML).render(**content) | |
with open('output.html','w') as f: | |
f.write(rendered_output) | |
options = { | |
'crop-h': 400, | |
'crop-w': 400, | |
'crop-x': 10, | |
'crop-y': 10, | |
} | |
imgkit.from_file('output.html', 'output.jpg', options=options) | |
pdfkit for from html to pdf.
https://github.com/JazzCore/python-pdfkit
pip install pdfkit
>>> import pdfkit
>>> pdfkit.from_file('output.html', 'output.pdf')
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sample output in jpg (could directly output pdf also).
backing up my own answer in case question below gets deleted.
https://stackoverflow.com/questions/53805697/write-on-images-using-python/53805948#53805948