Python Forum
New line in pdf? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: New line in pdf? (/thread-1599.html)

Pages: 1 2


New line in pdf? - panoss - Jan-15-2017

I 'm using reportlab for pdf creation..
My problem is new line, I tried \n and <br/> but failed.
What do you suggest?


RE: New line in pdf? - Larz60+ - Jan-15-2017

from stackoverflow use html <br/>


RE: New line in pdf? - panoss - Jan-15-2017

But it fails!


RE: New line in pdf? - Skaperen - Jan-16-2017

start a new stackoverflow question asking what to do when that fails, referencing that earlier answer.


RE: New line in pdf? - micseydel - Jan-16-2017

(Jan-16-2017, 05:58 AM)Skaperen Wrote: start a new stackoverflow question asking what to do when that fails, referencing that earlier answer.
We're suggesting a specific different forum now?

(Jan-15-2017, 05:52 PM)panoss Wrote: But it fails!
Can you provide a runnable snippet of code that reproduces the failure, and elaborate on "it fails"?


RE: New line in pdf? - panoss - Jan-17-2017

(Jan-16-2017, 06:10 AM)micseydel Wrote: Can you provide a runnable snippet of code that reproduces the failure, and elaborate on "it fails"?

Yes of course:

from reportlab.pdfgen import canvas
import os

c = canvas.Canvas("services_current_all.pdf")

output = "Serial number: " + "<br />"
output += "Model: " + "<br />"
output += "Other data: " + "<br />"

c.drawString(100, 650,output)
c.save()
os.system('services_current_all.pdf')



RE: New line in pdf? - micseydel - Jan-17-2017

Thank you for providing a runnable snippet, I've reproduced the problem. As context for others, "it fails" means that the generated PDF has the literal HTML rather than it being rendered as the OP expected; there is no error.

panoss: I've looked a bit, and it seems that the <br /> thing only works for Paragraphs. I couldn't get a paragraph to work with the canvas. I spent a few minutes Googling and trying code, and got the point where I think you should be doing that. Every piece of example code I could find had atrocious style, if you can use a different PDF generation tool (and I have no recommendations) then I suggest that route. Otherwise, spend some time digging into how to use a Paragraph, and I suspect that that will work based on a few different sources I came across.


RE: New line in pdf? - panoss - Jan-18-2017

I managed to print lines with this code:
import os
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch

c = canvas.Canvas("hello.pdf")

# move the origin up and to the left
c.translate(inch,inch)

textobject = c.beginText(0, 650)
textobject.setFont("Helvetica-Oblique", 14)
lines = ["line one", "line two"]
for line in lines:
    textobject.textLine(line)

c.drawText(textobject)

c.showPage()
c.save()

os.system('hello.pdf')
But I have problem with some Greek characters.
Vowels e.g α (alpha) without an accent mark, appear just fine.
But with an accent mark, e.g ά they are replaced by a small black rectangle.
How can I fix this?


RE: New line in pdf? - micseydel - Jan-18-2017

Again, always make sure you're posting code that reproduces the problem. I changed your code to include literals of those characters and then added an encoding and after that was able to reproduce the problem. I'm using vim over ssh and scp'ing the PDF each time I make an edit so you'll actually get responses faster if you post the right code.

From what I can tell, it seems that your font might not support the character. You have to find a font that supports all the characters you need.


RE: New line in pdf? - panoss - Jan-18-2017

micseydel you were right! It was the font!
I did this:
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont

pdfmetrics.registerFont(TTFont('Vera', 'Vera.ttf'))
pdfmetrics.registerFont(TTFont('VeraBd', 'VeraBd.ttf'))
pdfmetrics.registerFont(TTFont('VeraIt', 'VeraIt.ttf'))
pdfmetrics.registerFont(TTFont('VeraBI', 'VeraBI.ttf'))

c = canvas.Canvas("services_current_all.pdf")
c.setFont('Vera', 32)
And...worked!!!
Thank you all for your help!