docker run -it -u $(id -u):$(id -g) -w $PWD -v /mnt:/mnt pangyuteng/dcm:latest bash
apt-get install wkhtmltopdf
pip install imgkit,pdfkit,jinja2
Last active
June 3, 2024 22: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 hidden or 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
<!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=stock-photo-healthy-food-clean-eating-selection-fruit-vegetable-seeds-superfood-cereals-leaf-vegetable-on-722718082.jpg> | |
<img class="frame" src=grunge-circle-frame-1-1024x982.png> | |
<div class="circle">FOO<br>{{Sig_es_:signer1:signature}}</div> | |
</div> | |
</body> | |
</html> |
This file contains hidden or 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
import os | |
import traceback | |
import subprocess | |
if not os.path.exists("stock-photo-healthy-food-clean-eating-selection-fruit-vegetable-seeds-superfood-cereals-leaf-vegetable-on-722718082.jpg"): | |
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']) | |
if not os.path.exists("grunge-circle-frame-1-1024x982.png"): | |
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", | |
signature_field="{{Sig_es_:signer1:signature}}", | |
) | |
from jinja2 import Environment | |
import pdfkit | |
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}}<br>{{signature_field}}</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, | |
} | |
# import imgkit | |
# imgkit.from_file('output.html', 'output.jpg', options=options) | |
options = {'enable-local-file-access': None} | |
pdfkit.from_file('output.html', 'output.pdf',options=options) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pdfkit for from html to pdf.
https://github.com/JazzCore/python-pdfkit