runedisc
runedisc

Reputation: 60

Python reportlab paragraph function only draws a third of the input text to a pdf file

Forgive me if this is a common issue, i could not find an answer.

As the title states, I've got a problem with report lab drawing text to a .pdf file. The input is a big plaintext string, extracted from a json object.

This is the code i use too generate and automaticly open the pdf file. To open the document I use a system call.

def importAsPdf():
        #Open the plain tekst
        document = json.load(urllib2.urlopen(documenturl))
        documentId = document["id"].encode("utf-8")
        text = document["text"].encode("utf-8")
        print(text)

        #Create the pdf file
        doc = SimpleDocTemplate("pdftextfile.pdf")
        parts = []
        #setting page width and height. just used a standard A4 page measurement.
        PAGE_WIDTH, PAGE_HEIGHT = A4
        aW = PAGE_WIDTH - 4*inch  # available width and height 
        aH = PAGE_HEIGHT - 4*inch

        #importing the styles
        style = ParagraphStyle(name='fancy')
        style.fontSize = 12
        style.leading = 18

        #Build the pdf
        p = Paragraph(text, style)
        parts.append(p)
        doc.build(parts)
        print(doc.filename)
            #Open the pdf
        subprocess.call(('gnome-open', "pdftextfile.pdf"))

When it opens the pdf, only about on third of the text is in there and the other 2 thirds are no where to be found. It doesn't throw any exception or anything, it just stops somewhere mid sentence about 1 third of the way there.

Any thoughts?

edit: I've found that it stops at the only "<" in the text. Are these a problem for generating pdf files with reportlab?

Upvotes: 1

Views: 1144

Answers (2)

RmaxTwice
RmaxTwice

Reputation: 36

Yes, this happens to you because you can use XML tags inside the text to further format the text, so using your variables e.g:

text = """<font size=12>Hello<br/>World!</font>"""
p = Paragraph(text, style)
parts.append(p)
doc.build(parts)

That would produce the following paragraph with a font size of 12:

Hello

World!

So when the tags are incomplete ReportLab malfunctions. If you want to use angle brackets I suspect you must escape them from the given text. Cheers

Upvotes: 1

runedisc
runedisc

Reputation: 60

I have found that an "<" in the text stops reportlab from completely drawing the text to a pdf document. I removed the "<" with text.replace("<", "")and everything is now fine.

As to the why, I have no idea.

Upvotes: 0

Related Questions