使用xhtml2pdf将unicode模板转换为pdf的麻烦 [英] trouble in converting unicode template to pdf using xhtml2pdf

查看:207
本文介绍了使用xhtml2pdf将unicode模板转换为pdf的麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 #将HTML转换为PDF 
pisaStatus = pisa.CreatePDF(
StringIO(sourceHtml.encode(' utf-8')),
dest = resultFile)



完整的py代码:



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

from xhtml2pdf import pisa
from StringIO import StringIO

source =< html>
< style>
@ font-face {
font-family:Preeti;
src:url(preeti.ttf);
}

body {
font-family:Preeti;
}
< body>
这是一个测试< / b>
区域
< / body>
< / html>

#实用程序函数
def convertHtmlToPdf(source):
#打开输出文件进行写入(截断b inary)

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

#返回True成功,False出错
printSuccess:,pisaStatus.err
return pdf

#主程序
if __name __ ==__ main__:
print pisa.showLogging()
pdf = convertHtmlToPdf(source)
fd = open(test.pdf,w + b)
fd.write(pdf。 getvalue())
fd.close()



我是否还需要包含font-face?其中部分解决了。提供字体的绝对路径,即

 < style> 
@ font-face {
font-family:Preeti;
src:url(c:/static/fonts/preeti.ttf);
}

body {
font-family:Preeti;
}
< / style>

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

I have unicode used in my html page, which is displaying correctly in the html page. But while converting it into html using xhtml2pdf, it generating black, solid square boxes in the unicodes. Is there some setting for unicode other than UTF-8 setting. I dont think its unicode problem.

# convert HTML to PDF
pisaStatus = pisa.CreatePDF(
        StringIO(sourceHtml.encode('utf-8')),                 
        dest=resultFile)

Complete py code:

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

from xhtml2pdf import pisa
from StringIO import StringIO

source = """<html>
            <style>
                @font-face {
                font-family: Preeti;
                src: url("preeti.ttf");
                }

                body {
                font-family: Preeti;
                }
            </style>
            <body>
                This is a test <br/>
                       सरल
            </body>
        </html>"""

# 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()

Do I even Need to include the font-face ??

解决方案

Its partially solved. Providing the absolute path to the font i.e.

    <style>
        @font-face {
        font-family: Preeti;
        src: url("c:/static/fonts/preeti.ttf");
        }

        body {
        font-family: Preeti;
        }
    </style>  

Now another problem has raised. I have mixed texts, partially in unicode and partially in normal Font(I think I should say it normal fonts :D), since fonts have been overridden, now the normal Fonts are coming in rectangular boxes. In this case a empty box.

这篇关于使用xhtml2pdf将unicode模板转换为pdf的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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