xhtml2pdf,西里尔字符不起作用

时间:2015-12-08 14:41:47

标签: python html pdf pdf-generation xhtml2pdf

我尝试将html转换为pdf。我用的是xhtml2pdf。 我的代码:

# -*- coding: utf-8 -*-
from xhtml2pdf import pisa
sourceHtml = '<html>' \
             '    <head>' \
             '        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">' \
             '    </head>' \
             '    <body>' \
             '        <p>Русский текст</p>' \
             '    </body>' \
             '</html>'

outputFilename = "test.pdf"

def convertHtmlToPdf(sourceHtml, outputFilename):
    resultFile = open(outputFilename, "w+b")
    pisaStatus = pisa.CreatePDF(sourceHtml, dest=resultFile)#, encoding='UTF-8')
    resultFile.close() 
    return pisaStatus.err

if __name__ == "__main__":
    pisa.showLogging()
    convertHtmlToPdf(sourceHtml, outputFilename)

创建PDF,但西里尔字符由黑色方块表示。 我做错了什么?怎么做对了?

1 个答案:

答案 0 :(得分:2)

# -*- coding: utf-8 -*-
from xhtml2pdf import pisa
sourceHtml = '<html>' \
             '    <head>' \
             '        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">' \
             '        <style type="text/css">' \
             '            @page { size: A4; margin: 1cm; }' \
             '            @font-face { font-family: Arial; src: url(/pathToTTF/arial.ttf); }' \
             '            p { color: red; font-family: Arial; }' \
             '        </style>' \
             '    </head>' \
             '    <body>' \
             '        <p>Русский текст</p>' \
             '    </body>' \
             '</html>'

outputFilename = "test.pdf"

def convertHtmlToPdf(sourceHtml, outputFilename):
    resultFile = open(outputFilename, "w+b")
    pisaStatus = pisa.CreatePDF(sourceHtml, dest=resultFile, encoding='UTF-8')
    resultFile.close() 
    return pisaStatus.err

if __name__ == "__main__":
    pisa.showLogging()
    convertHtmlToPdf(sourceHtml, outputFilename)

有效。我必须将测试样式设置为html标记。