Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reportlab new line in a long line

enter image description here

I need a new line, so I can see a format in the PFD, I try to do it adding a page width but it didn work, I use an other thing with /n and it didn't work nither. This is my code. I can add a format manualy cause I need to show information that a get from the database and I get the information in one long line.

def PdfReportView(request):
    print id
    # Create the HttpResponse object with the appropriate PDF headers.
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'filename="PatientReport.pdf"'
    c = canvas.Canvas(response, pagesize=letter)
    t = c.beginText()
    t.setFont('Helvetica-Bold', 10)
    t.setCharSpace(3)
    t.setTextOrigin(50, 700)
    t.textLine("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.")
    c.drawText(t)
    c.showPage()
    c.save()
    return response
like image 806
GioBot Avatar asked Dec 02 '22 14:12

GioBot


1 Answers

You can use textLines() if you have \n in your text input:

t.textLines('''Lorem Ipsum is simply dummy text of the printing and 
typesetting industry. Lorem Ipsum has been the industry's standard dummy text 
ever since the 1500s, when an unknown printer took a galley of type and 
scrambled it to make a type specimen book.''')

if your text is one line, you can use textwrap module to break it to several lines:

from textwrap import wrap

text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."

wraped_text = "\n".join(wrap(text, 80)) # 80 is line width

t.textLines(wraped_text)
like image 110
ahmed Avatar answered Dec 06 '22 09:12

ahmed