python-3.x - 如何编码此字符串以将其导出为 PDF
问题描述
我正在尝试将包含 UTF-8 字符的字符串导出到 PDF。我已将我的代码简化为以下代码,以便能够重现它:
source = """<html>
<style>
@font-face {
font-family: Preeti;
src: url("preeti.ttf");
}
body {
font-family: Preeti;
}
</style>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>
This is a test <br/>
सरल
</body>
</html>"""
from xhtml2pdf import pisa
from io import StringIO, BytesIO
bytes_result = BytesIO()
pisa.CreatePDF(source, bytes_result, encoding='UTF-8')
result = StringIO(bytes_result.getvalue().decode('UTF-8', 'ignore'))
f = open('test.pdf', 'wt', encoding='utf-8')
f.write(result.getvalue())
生成以下PDF:
我从互联网上尝试了很多解决方案,但我无法正确解决。知道我错过了什么吗?
解决方案
推荐阅读
- javascript - 地图功能不渲染元素
- javascript - Angular:路由器导航引发 JIT 编译失败
- c - 如何计算结构中数据成员的大小?
- c# - SSRSReport打印服务——无效的报表定义错误随机出现,重启服务后才能再次工作
- python - args[0]['valid'] 在装饰器中是什么意思
- mysql - 合并两个有另一个关系表的表
- c# - C#静默安装msi不起作用
- google-cloud-platform - 在 GCP-我如何获得所有价格的列表
- javascript - 内部在猫鼬中加入 2 个表
- angular - 使用 ngx-bootstrap 将任何组件传递给我的模态