如何使用python-pdfkit中的from_string生成具有非ascii字符的PDF [英] How to generate a PDF with non-ascii characters using from_string from python-pdfkit

查看:454
本文介绍了如何使用python-pdfkit中的from_string生成具有非ascii字符的PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使用Python 3.5.2,python-pdfkit和wkhtmltox-0.12.2生成带有非ASCII字符的简单PDF。

I'm struggling to generate just a simple PDF with non-ascii characters using Python 3.5.2, python-pdfkit and wkhtmltox-0.12.2.

是我可以写的最简单的例子:

This is the easiest example I could write:

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

这就像输出文档如下所示:

This is like the output document looks like:

推荐答案

我只需要向我的HTML代码中添加一个具有charset属性的元标记:

I found out that I just needed to add a meta tag with charset attribute to my HTML code:

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')

我实际上花了很多时间来遵循错误的解决方案,例如这里建议的解决方案。如果有人感兴趣,我写了一个短篇小说在我的博客上。对垃圾邮件表示抱歉:)

I actually spent quite some time following wrong solutions like the one suggested here. In case someone is interested, I wrote a short story on my blog. Sorry for the SPAM :)

这篇关于如何使用python-pdfkit中的from_string生成具有非ascii字符的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆