ReportLab

From XPUB & Lens-Based wiki
Revision as of 16:03, 31 January 2018 by Michael Murtaugh (talk | contribs) (Michael Murtaugh moved page Creating a PDF with Python to ReportLab)

ReportLab

Following this helpful tutorial

from reportlab.pdfgen import canvas
 
c = canvas.Canvas("hello.pdf")
c.drawString(100, 100, "Hello WORLD!")
c.save()

Creates a pdf from a .txt file.

#!/usr/bin/py
#gen pdf


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

doc = SimpleDocTemplate("text.pdf", pagesize=A4,
                        rightMargin=72, leftMargin=72,
                        topMargin=72, bottomMargin=18)


content = []
styles = getSampleStyleSheet()

for line in open('file.txt'):
    line = line.strip()
    if line:
        p = Paragraph('<font size=12>'+line+'</font>', styles["Normal"])
        content.append(p)
        content.append(Spacer(1, 12))

doc.build(content)