beebek beebek - 5 months ago 100
Python Question

trouble in converting unicode template to pdf using xhtml2pdf

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(

Complete py code:

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

from xhtml2pdf import pisa
from StringIO import StringIO

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

body {
font-family: Preeti;
This is a test <br/>

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

generated pdf file

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


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

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

        body {
        font-family: Preeti;

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.