python将html转成PDF,包含中文

前提:

安装xhtml2pdf

下载字体:code2000.ttf;给个地址:http://ishare.iask.sina.com.cn/f/22120225.html

待转换的文件: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>文字</td>
<td>123</td>
</tr>
<tr>
<td>图片</td>
<td><img src="1.jpg"></td>
</tr>
</table></body></html>

html_to_pdf.py程序

# -*- 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不能识别汉字,需要在html文件中通过CSS的方式嵌入code2000字体,貌似只能用code2000,原因不明。

你可能感兴趣的:(html,XHTML,python,css,File,import)