ReportLab: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
Line 42: Line 42:
doc.build(content)
doc.build(content)
</source>
</source>
[[Category:Cookbook]] [[Category:Python]] [[Category:PDF]]

Revision as of 22:08, 14 November 2012

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)