htmlから非ASCII文字を含むpdfを作成しようとするとエラーが発生するソフトウェアがあります。私は問題を再現し、何が起こっているのか理解するのを助けるもっと簡単なプログラムを作成しました。ASCII以外の文字でpdfを生成しようとするとUnicodeのデコードエラーが発生する
でこのプログラムの結果を実行#!/usr/bin/python
#coding=utf8
from __future__ import unicode_literals
import pdfkit
from pyPdf import PdfFileWriter, PdfFileReader
f = open('test.html','r')
html = f.read()
print html
pdfkit.from_string(html, 'gen.pdf')
f.close()
:私は、問題の文字を取り除くために置き換える文を追加しようとしたが、それはまた、エラーになった
<html>
<body>
<h1>ر</h1>
</body>
</html>
Traceback (most recent call last):
File "./testerror.py", line 10, in <module>
pdfkit.from_string(html, 'gen.pdf')
File "/usr/local/lib/python2.7/dist-packages/pdfkit/api.py", line 72, in from_string
return r.to_pdf(output_path)
File "/usr/local/lib/python2.7/dist-packages/pdfkit/pdfkit.py", line 136, in to_pdf
input = self.source.to_s().encode('utf-8')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd8 in position 18: ordinal not in range(128)
:
Traceback (most recent call last):
File "./testerror.py", line 9, in <module>
html = html.replace('ر','-')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd8 in position 18: ordinal not in range(128)
私は怖いです私はascii/utf-8エンコーディングがうまく理解できません。もし誰かが私がここで何が起こっているのか理解するのを助けることができれば、それは素晴らしいだろう!これは、PDFライブラリで問題となっている、またはこれはpdfkit
ソースコードを読むエンコーディング:)
「私は、私は非常によく、ASCII/UTF-8エンコーディングを理解していない怖い」=>その後、最初にこれを読む:http://kunststube.net/encoding/リンクの –
感謝を。私はそれを読んだ。しかし、私はまだこの特定の状況にいくつかの助けに感謝します。 – Daniel
あなたのtest.htmlファイルはどのようなエンコーディングですか?明らかにpdfkitはUnicode文字列を期待しているので、pdfkitに渡す前にそれを解読するのはあなたの義務です。 –