xhtml2pdf: black square box while generating pdf (unicode error)

A weird problem. While generating pdf, inplace of unicodes square black boxes apperars. Dont know if its unicode or font-face error. I even dont know if to use the “font-face and font-family” to generate the unicode into pdf. Anything I am missing ?? Great thanks.

Code snippet # -- coding: utf-8 --

from xhtml2pdf import pisa
from StringIO import StringIO

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

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

# Utility function
def convertHtmlToPdf(source):       
    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()

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 31 (2 by maintainers)

Most upvoted comments

yes it’s work but if you put some russian chars, it creates black squares the problem is how we can use non english chars?

#coding: utf-8

from xhtml2pdf import pisa

sourceHtml = "<html>" \
                 "<style>" \
                    "@font-face {" \
                        "font-family: 'DejaVu Sans';" \
                        "src: url('C:/Users/lazarev/PycharmProjects/zmtek/zmtek/collected_static/fonts/DejaVuSans.ttf');" \
                    "}" \
                    "html,body{" \
                        "font-size: 12pt;" \
                        "font-family: 'DejaVu Sans';" \
                    "}" \
              "</style>" \
              "<body><p>А что на счет Русских букв?<p></body>" \
          "</html>"
outputFilename = "test.pdf"

# Utility function
def convertHtmlToPdf(sourceHtml, outputFilename):
    resultFile = open(outputFilename, "w+b")

    # convert HTML to PDF
    pisaStatus = pisa.CreatePDF(
            sourceHtml,                # the HTML to convert
            dest=resultFile)           # file handle to recieve result

    # close output file
    resultFile.close()                 # close output file

    # return True on success and False on errors
    return pisaStatus.err

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

It’s very disappoiting to add local fonts for any HTML document to render in non-latin encoding