Home >Backend Development >Python Tutorial >python将html转成PDF的实现代码(包含中文)

python将html转成PDF的实现代码(包含中文)

WBOY
WBOYOriginal
2016-06-16 08:46:441557browse

前提:

安装xhtml2pdf https://pypi.python.org/pypi/xhtml2pdf/
下载字体:微软雅黑;给个地址:http://www.jb51.net/fonts/8481.html

待转换的文件:1.htm

复制代码 代码如下:













文字 123
图片 python将html转成PDF的实现代码(包含中文)

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,原因不明。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn