question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

black square box while generating pdf (unicode error)

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:31 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
sunnybearcommented, Jul 21, 2019

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

1reaction
lazareveugenecommented, Sep 6, 2013

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)
Read more comments on GitHub >

github_iconTop Results From Across the Web

trouble in converting unicode template to pdf using xhtml2pdf
But while converting it into html using xhtml2pdf, it generating black, solid square boxes in the unicodes.
Read more >
What symbol is the square box shown for non-representable ...
U+FFFD REPLACEMENT CHARACTER, , is used to signal errors in invalid Unicode streams; it's usually rendered as a question mark inside a...
Read more >
Mouse pointer changes into a black box when moved over ...
If your mouse cursor changes into a black box when moved over blank areas in a PDF document, use the solution below as...
Read more >
When I print to Adobe PDF, strange symbols appear in the ...
Lookfor IMAGE COLOR MANAGEMENT, TRUE TYPE FONT: Clickon the “Substitute with device font.” A dropdown box will appear. Click on “DOWNLOAD AS SOFTFONT.”....
Read more >
Why does Notepad++ display some Unicode characters as ...
It can display ₐ₊₋ but some other subscripts (like ₂ₙ ) are converted to squares. When I copy that square and paste on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found