如何使用pythonpdfki中的from_字符串生成包含非科学字符的PDF

2024-09-29 19:19:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力使用python3.5.2、pythonpdfkit和wkhtmltox-0.12.2生成一个包含非ascii字符的简单PDF。在

这是我能写的最简单的例子:

import pdfkit
html_content = u'<p>ö</p>'
pdfkit.from_string(html_content, 'out.pdf')

输出文档如下所示:non-ascii character incorrectly shown in the PDF


Tags: from文档importstringpdfhtmlasciicontent
2条回答

pdfkit项目中存在相关问题https://github.com/devongovett/pdfkit/issues/470 这说明

"You need to use an embedded font. The built-in fonts have a limited character set available."

这个问题的答案How to: output Euro symbol in pdfkit for nodejs给出了一个如何做的线索。在

我发现我只需要在HTML代码中添加一个具有charset属性的meta标记:

import pdfkit

html_content = """
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
</head>
<body>
    <p>&euro;</p>
    <p>áéíóúñö</p>
<body>
</html>
"""

pdfkit.from_string(html_content, 'out.pdf')

事实上,我花了相当长的时间遵循错误的解决方案,就像这里建议的那样。如果有人感兴趣,我写了一篇短篇小说on my blog。抱歉,垃圾邮件:)

相关问题 更多 >

    热门问题