Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New line in pdf?
#1
I 'm using reportlab for pdf creation..
My problem is new line, I tried \n and <br/> but failed.
What do you suggest?
Reply
#2
from stackoverflow use html <br/>
Reply
#3
But it fails!
Reply
#4
start a new stackoverflow question asking what to do when that fails, referencing that earlier answer.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
(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"?
Reply
#6
(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')
Reply
#7
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.
Reply
#8
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?
Reply
#9
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.
Reply
#10
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!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020