Jim
Jim

Reputation: 875

PDF generation in python

I'm trying to generate a PDF file instead of a text file for a program I've created.

My issue is I've looked a reportlab and it seems overly complicated for what I need, as all I want is to essentially print to a pdf instead of a text file.

current test code, it works but I'm confused about positioning and all the lines run of the end of the page, could someone please advise me on how positioning works with reportlab

from reportlab.pdfgen import canvas
def genText():
    text =["Call me Ishmael.",
        "Some years ago- never mind how long precisely- having little or no money in my purse,",
        "and nothing particular to interest me on shore,",
        "I thought I would sail about a little and see the watery part of the world."]
    return text

def testText(page,text):

    from reportlab.lib.units import inch

    textobject = page.beginText()
    textobject.setTextOrigin(inch, 2.5*inch)
    textobject.setFont("Helvetica-Oblique", 14)
    for line in text:
        textobject.textLine(line)

    page.drawText(textobject)    

page = canvas.Canvas("JIMTEST.pdf")
text = genText()
testText(page, text)
page.showPage()
page.save()

Data that is actually output by my program:

---------------------------------------------------------
Milk Company: Bobbys Milk
Haulier: Jims Hauliers
Truck: T55JHH

Driver: 123     Route: 852
    Joe Bloggs

Everyday Collection

MilkType: Ordinary

---------------------------------------------------------
Last TankWash

Start Time: 2016/03/31 13:30:32
Finsished:  14:21:03
Litres: 9451
Temperature: 70.0 deg_C

   770500  CREAMERY

---------------------------------------------------------
Locn                    Litres

   770083 Wyrill
Coll        1643   2.0  deg_C   smp 143
2016/04/01 06:40:28

   770084 Foster
Coll        2242   1.0  deg_C   smp 28
2016/04/01 07:17:57

   770080 Dugdale
Coll        8237   4.0  deg_C   smp 49
2016/04/01 08:02:39

   770086 Cragg
Coll        4591   1.7  deg_C   smp 68
2016/04/01 09:00:17

   770051 D & S Spence
Coll        2868   3.7  deg_C   smp 83
2016/04/01 10:06:11

   770500 CREAMERY
delyFZ  -19581
Tank#     0 Ower#  3805
2016/04/01 11:14:11


---------------------------------------------------------
Milk Collected: 19581
Milk OnBoard: 0
---------------------------------------------------------
Estimated Print Time at: 2016/04/01 11:14:16

Thanks

Upvotes: 0

Views: 1336

Answers (2)

Felipe Sierra
Felipe Sierra

Reputation: 197

You can check pdfme library. It's a powerful library in python to create PDF documents, and it's very easy to use.

To "print" those logs into a PDF document you would do this:

from pdfme import build_pdf

with open("logs.pdf", 'wb') as f:
    build_pdf({
        "style": {"s": 12, "text_aling": "j"},
        "sections": [
            {"content": [LOGS_VARIABLE_STR]}
        ]
    }, f)

And that would be it. You can also split the string, give it some format if you want, and add the formatted paragraphs following the instructions in the docs (https://pdfme.readthedocs.io/)

Upvotes: 0

Mike Scotty
Mike Scotty

Reputation: 10782

I sugggest you look at the platypus part of reportlab.

The Paragraph acts pretty much like a DIV container in HTML and will wrap text automatically at the end of the document.

from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle

def genText():
    text = ["Call me Ishmael.",
        "Some years ago- never mind how long precisely- having little or no money in my purse,",
        "and nothing particular to interest me on shore,",
        "I thought I would sail about a little and see the watery part of the world."]
    return text

styles = getSampleStyleSheet()
doc = SimpleDocTemplate("my_doc.pdf", pagesize=A4)
Story=[]
text = genText()
for t in text:
    Story.append(Paragraph(t, styles["Normal"]))
Story.append(Spacer(10, 10))
t = "lorem ipsum " * 100
Story.append(Paragraph(t, styles["Normal"]))
doc.build(Story)

Of couse you can also mix canvas and platypus - this is just a minimal example to get you going.

To position your elements I suggest you look at the platypus Table.

Upvotes: 2

Related Questions