ehsan-dashboards/dashboards/VPN_chanel/test_resvg.py

48 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import resvg_py
import arabic_reshaper
from bidi.algorithm import get_display
import base64
with open('/workspace/personal/dashboards/VPN_chanel/backend/app/static/fonts/Vazirmatn-Bold.ttf', 'rb') as f:
font_b64 = base64.b64encode(f.read()).decode('utf-8')
text_farsi = get_display(arabic_reshaper.reshape("ارتقای لول کارها با هوش مصنوعی"))
price_farsi = get_display(arabic_reshaper.reshape("تخفیف ویژه: ۶۰۰,۰۰۰ تومان"))
svg = f'''<svg xmlns="http://www.w3.org/2000/svg" width="1080" height="1080" viewBox="0 0 1080 1080">
<defs>
<style>
@font-face {{
font-family: 'Vazirmatn';
src: url(data:font/truetype;charset=utf-8;base64,{font_b64}) format('truetype');
font-weight: bold;
}}
.farsi {{
font-family: 'Vazirmatn', sans-serif;
text-anchor: middle;
}}
</style>
<linearGradient id="bg" x1="0%" y1="0%" x2="100%" y2="100%">
<stop offset="0%" stop-color="#0B0F19"/>
<stop offset="50%" stop-color="#111827"/>
<stop offset="100%" stop-color="#080C14"/>
</linearGradient>
<linearGradient id="gold" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" stop-color="#F59E0B"/>
<stop offset="50%" stop-color="#FBBF24"/>
<stop offset="100%" stop-color="#D97706"/>
</linearGradient>
</defs>
<rect width="1080" height="1080" fill="url(#bg)"/>
<text x="540" y="200" fill="#FFFFFF" font-size="48" class="farsi">{text_farsi}</text>
<rect x="290" y="500" width="500" height="90" rx="45" fill="url(#gold)"/>
<text x="540" y="558" fill="#000000" font-size="36" class="farsi">{price_farsi}</text>
</svg>'''
png_bytes = resvg_py.svg_to_bytes(svg)
with open('/tmp/test_resvg_farsi.png', 'wb') as f:
f.write(png_bytes)
print("Rendered PNG, size:", len(png_bytes))