源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-09-03 14:27 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:python将html转成PDF的实现代码(包含中文)
前提: 安装xhtml2pdf [url=https://pypi.python.org/pypi/xhtml2pdf/]https://pypi.python.org/pypi/xhtml2pdf/[/url] 下载字体:微软雅黑;给个地址:[url=http://www.1sucai.cn/fonts/8481.html]http://www.1sucai.cn/fonts/8481.html[/url] 待转换的文件:1.htm
[u]复制代码[/u] 代码如下:
<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>
[b]html_to_pdf.py程序[/b]
[u]复制代码[/u] 代码如下:
# -*- 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,原因不明。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部