Skip to content

Instantly share code, notes, and snippets.

@pangyuteng
Last active June 3, 2024 22:06
Show Gist options
  • Save pangyuteng/2af5d4ee692f05fab20e72630260e2be to your computer and use it in GitHub Desktop.
Save pangyuteng/2af5d4ee692f05fab20e72630260e2be to your computer and use it in GitHub Desktop.
report (image,text) generation in python using jinja2,html,wkhtmltopdf via imgkit


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

<!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>
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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)
@pangyuteng
Copy link
Author

pangyuteng commented Dec 16, 2018

output
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

@pangyuteng
Copy link
Author

pangyuteng commented Jul 24, 2021

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