python html to pdf centos8

พอดีมีงานที่ต้องทำ HTML เป็น PDF ด้วย python เลยลงไว้กันลืมสักหน่อย  ปกติทำแต่บน php

 

ขั้นแรกให้ติดตั้งตัวแปล html to pdf  ของ centos8 ก่อน

wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox-0.12.6-1.centos8.x86_64.rpm

dnf localinstall wkhtmltox-0.12.6-1.centos8.x86_64.rpm

จากนั้น pip บน python ในที่นี้ผมใช้ python3.7

pip3.7 install pdfkit

ต่อมา เรื่องภาษาไทย ให้ติดตั้งตามนี้

dnf install thai-scalable-tlwgmono-fonts

 

จากนั้นลอง code ตามนี้

import pdfkit

# from HTML content
html=”””
<head>
<meta charset=’utf-8′ />
</head>
<p><b>Python</b> is a great programming languageคนน่ารักใครๆก็คงอยากรักถ้ามันไม่ผิดมากนัก.</p>
“””
pdfkit.from_string(html, “/usr/share/nginx/html/pdf_file/string.pdf”, verbose=True)
print(“=”*50)

 

จากนั้นลองทดสอบดู ในส่วนของ <meta charset=’utf-8′ /> ต้องมีไม่งั้นภาษาไทยจะไม่ได้

 

 

 

ใส่ความเห็น

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องที่ต้องการถูกทำเครื่องหมาย *