ReportLab: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with " == ReportLab == Following this [http://www.blog.pythonlibrary.org/2010/03/08/a-simple-step-by-step-reportlab-tutorial/ helpful tutorial] <source lang="python"> from reportl...")
 
No edit summary
Line 10: Line 10:
c.drawString(100, 100, "Hello WORLD!")
c.drawString(100, 100, "Hello WORLD!")
c.save()
c.save()
</source>
Creates a pdf from a .txt file.
<source lang="python">
#!/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)
</source>
</source>

Revision as of 17:00, 13 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)