当前位置: 首页 > 工具软件 > xhtml2pdf > 使用案例 >

python unicode 编程 pdf_python – 使用xhtml2pdf将unicode模板转换为pdf的麻烦

严阳秋
2023-12-01

我在我的html页面中使用了unicode,它在html页面中正确显示.但是在使用xhtml2pdf将其转换为html时,它会在unicodes中生成黑色实心方框.是否有UTF-8设置以外的unicode设置.我不认为它的unicode问题.

# convert HTML to PDF

pisaStatus = pisa.CreatePDF(

StringIO(sourceHtml.encode('utf-8')),

dest=resultFile)

完整的py代码:

# -*- coding: utf-8 -*-

from xhtml2pdf import pisa

from StringIO import StringIO

source = """

@font-face {

font-family: Preeti;

src: url("preeti.ttf");

}

body {

font-family: Preeti;

}

This is a test

सरल

"""

# Utility function

def convertHtmlToPdf(source):

# open output file for writing (truncated binary)

pdf = StringIO()

pisaStatus = pisa.CreatePDF(StringIO(source.encode('utf-8')), pdf)

# return True on success and False on errors

print "Success: ", pisaStatus.err

return pdf

# Main program

if __name__=="__main__":

print pisa.showLogging()

pdf = convertHtmlToPdf(source)

fd = open("test.pdf", "w+b")

fd.write(pdf.getvalue())

fd.close()

我甚至需要包括font-face?

解决方法:

它部分解决了.提供字体的绝对路径,即

@font-face {

font-family: Preeti;

src: url("c:/static/fonts/preeti.ttf");

}

body {

font-family: Preeti;

}

现在又出现了另一个问题.我有混合文本,部分在unicode和部分正常字体(我想我应该说它是普通字体:D),因为字体已被覆盖,现在正常的字体是在矩形框中.在这种情况下一个空盒子.

标签:python,unicode,flask,xhtml2pdf

 类似资料: