Python's implementation code of converting HTML to PDF contains Chinese
- 2020-04-02 09:52:03
- OfStack
Premise:
Install xhtml2pdf (link: https://pypi.python.org/pypi/xhtml2pdf/)
Download font: Microsoft yahei; Give me an address:
File to be converted: 1.htm
<meta charset="utf8"/>
<style type='text/css'>
@font-face {
font-family: "code2000";
src: url("code2000.ttf")
}
html {
font-family: code2000;
}
</style>
<html>
<body><table>
<tr>
<td> The text </td>
<td>123</td>
</tr>
<tr>
<td> The picture </td>
<td><img src="1.jpg"></td>
</tr>
</table></body></html>
Html_to_pdf. Py program
# -*- coding: utf-8 -*-
import sx.pisa3 as pisa
data= open('1.htm').read()
result = file('test.pdf', 'wb')
pdf = pisa.CreatePDF(data, result)
result.close()
pisa.startViewer('test.pdf')
Xhtml2pdf cannot recognize Chinese characters, so it is necessary to embed code2000 font in HTML file by CSS. It seems that code2000 can only be used, for unknown reasons.