我尝试将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,但西里尔字符由黑色方块表示。 我做错了什么?怎么做对了?
答案 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标记。