ReportLab: Difference between revisions

From XPUB & Lens-Based wiki
m (Michael Murtaugh moved page Creating a PDF with Python to ReportLab)
No edit summary
Line 1: Line 1:
== ReportLab ==


Following this [http://www.blog.pythonlibrary.org/2010/03/08/a-simple-step-by-step-reportlab-tutorial/ helpful tutorial]
Following this [http://www.blog.pythonlibrary.org/2010/03/08/a-simple-step-by-step-reportlab-tutorial/ helpful tutorial]
Line 42: Line 40:
doc.build(content)
doc.build(content)
</source>
</source>
From the Report Lab How to PDF:
<blockquote>The pdfgen package is the lowest level interface for generating PDF documents. A pdfgen program is essentially a sequence of instructions for &quot;painting&quot; a document onto a sequence of pages. The interface object which provides the painting operations is the pdfgen canvas.
</blockquote>
The Canvas constructor:
<pre>def __init__(self,filename,
    pagesize=(595.27,841.89),
    bottomup = 1,
    pageCompression=0,
    encoding=rl_config.defaultEncoding,
    verbosity=0
    encrypt=None):</pre>
== Basic page operations ==
<source lang="python">from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.pagesizes import letter, A4
from reportlab.lib.units import inch, cm
import sys
from reportlab.pdfbase.ttfonts import TTFont, pdfmetrics
fontpath = "OSP-DIN.ttf"
font =  TTFont('MyFontName', fontpath)
pdfmetrics.registerFont(font)
c = Canvas("pageloop.pdf", pagesize=A4)
for i in range(1000):
    c.setPageSize(A4)
    c.setFont('MyFontName', 72)
    # c.drawString(10*cm, 0.5*cm, "Page {0}".format(i))
    c.drawCentredString(A4[0]/2, A4[1]/2, "Page {0}".format(i))
    c.showPage()
c.save()
</source>
== Differing page sizes ==
<source lang="python">from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.pagesizes import A4, A5
from reportlab.lib.units import cm
from reportlab.pdfbase.ttfonts import TTFont, pdfmetrics
import sys
pdfmetrics.registerFont(TTFont('din', "OSP-DIN.ttf"))
pdfmetrics.registerFont(TTFont('lib', "Libertinage-x.ttf"))
c = Canvas("pageloopdifferentsizes.pdf", pagesize=A4)
for i in range(1000):
    if i%2==0:
        ps = A4
        c.setFont('din', 144)   
    else:
        ps = A5
        c.setFont('lib', 72)
    c.setPageSize(ps)   
    # c.drawString(10*cm, 0.5*cm, "Page {0}".format(i))
    c.drawCentredString(ps[0]/2, ps[1]/2, "Page {0}".format(i))
    c.showPage()
c.save()
</source>
== Flows (Platypus) ==
Example: Jekyll &amp; Hyde meet Platypus
<source lang="python">from reportlab.platypus import *
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.rl_config import defaultPageSize
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import inch, cm
import sys
import html5lib
leftPadding = 6
rightPadding = 6
bottomPadding = 6
topPadding = 6
pagew, pageh = A4
framew = (pagew / 2) - leftPadding - rightPadding
frameh = pageh - topPadding - bottomPadding
leftColumn = Frame(0, 0, framew, frameh, leftPadding, bottomPadding, rightPadding, topPadding, id="leftColumn")
rightColumn = Frame((pagew/2), 0, framew, frameh, leftPadding, bottomPadding, rightPadding, topPadding, id="rightColumn")
styles = getSampleStyleSheet()
elements = []
def header(txt):
    s = Spacer(0.2*inch, 0.3*inch)
    elements.append(s)
    para = Paragraph(txt, styles["Heading1"])
    elements.append(para)
def p(txt):
    s = Spacer(0.2*inch, 0.1*inch)
    elements.append(s)
    para = Paragraph(txt, styles["Normal"])
    elements.append(para)
# NOT USED
# def pre(txt):
#    s = Spacer(0.1*inch, 0.1*inch)
#    elements.append(s)
#    p = Preformatted(txt, styles["Code"])
#    elements.append(p)
# Read the HTML and "perform" the h1 and p elements
with open("jekyll.html") as f:
    t = html5lib.parse(f, treebuilder="etree", namespaceHTMLElements=False)
    for elt in t.find(".//body"):
        if elt.tag == "h1":
            header(elt.text)
        elif elt.tag == "p":
            p(elt.text)
twoColumnPage = PageTemplate(id="base", frames=[leftColumn, rightColumn], pagesize=A4)
doc = BaseDocTemplate("jekyll.pdf", pageTemplates=[twoColumnPage])
# elements.insert(0,Spacer(0,inch))
# doc = SimpleDocTemplate('gfe.pdf')
doc.build(elements)
</source>
Result: [[jekyll/jekyll.pdf|jekyll.pdf]]
== Example: SICV Random Walk ==
http://guttormsgaard.activearchives.org/cgi-bin/walk.cgi
== RML ==
RMLL is a report-lab specific document markup language similar to HTML but much more limited (and specific to the capabilities of Report Lab). The &quot;official&quot; tools to process RML and produce PDFs are commercial, but there exist alternative open source implementations based on the open source report lab library.
* [https://pypi.python.org/pypi/trml2pdf/0.1 trml2pdf] [https://github.com/romanlv/trml2pdf gitlab]
* [https://pypi.python.org/pypi/z3c.rml z3c]




[[Category:Cookbook]] [[Category:Python]] [[Category:PDF]]
[[Category:Cookbook]] [[Category:Python]] [[Category:PDF]]

Revision as of 16:03, 31 January 2018

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)

From the Report Lab How to PDF:

The pdfgen package is the lowest level interface for generating PDF documents. A pdfgen program is essentially a sequence of instructions for "painting" a document onto a sequence of pages. The interface object which provides the painting operations is the pdfgen canvas.

The Canvas constructor:

def __init__(self,filename,
    pagesize=(595.27,841.89),
    bottomup = 1,
    pageCompression=0,
    encoding=rl_config.defaultEncoding,
    verbosity=0
    encrypt=None):

Basic page operations

from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.pagesizes import letter, A4
from reportlab.lib.units import inch, cm
import sys
from reportlab.pdfbase.ttfonts import TTFont, pdfmetrics

fontpath = "OSP-DIN.ttf"
font =  TTFont('MyFontName', fontpath)
pdfmetrics.registerFont(font)
c = Canvas("pageloop.pdf", pagesize=A4)

for i in range(1000):
    c.setPageSize(A4)
    c.setFont('MyFontName', 72)
    # c.drawString(10*cm, 0.5*cm, "Page {0}".format(i))
    c.drawCentredString(A4[0]/2, A4[1]/2, "Page {0}".format(i))
    c.showPage()
c.save()

Differing page sizes

from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.pagesizes import A4, A5
from reportlab.lib.units import cm
from reportlab.pdfbase.ttfonts import TTFont, pdfmetrics
import sys


pdfmetrics.registerFont(TTFont('din', "OSP-DIN.ttf"))
pdfmetrics.registerFont(TTFont('lib', "Libertinage-x.ttf"))
c = Canvas("pageloopdifferentsizes.pdf", pagesize=A4)

for i in range(1000):
    if i%2==0:
        ps = A4
        c.setFont('din', 144)    
    else:
        ps = A5
        c.setFont('lib', 72)
    c.setPageSize(ps)    
    # c.drawString(10*cm, 0.5*cm, "Page {0}".format(i))
    c.drawCentredString(ps[0]/2, ps[1]/2, "Page {0}".format(i))
    c.showPage()
c.save()

Flows (Platypus)

Example: Jekyll & Hyde meet Platypus

from reportlab.platypus import *
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.rl_config import defaultPageSize

from reportlab.lib.pagesizes import A4
from reportlab.lib.units import inch, cm
import sys
import html5lib


leftPadding = 6
rightPadding = 6
bottomPadding = 6
topPadding = 6

pagew, pageh = A4
framew = (pagew / 2) - leftPadding - rightPadding
frameh = pageh - topPadding - bottomPadding
leftColumn = Frame(0, 0, framew, frameh, leftPadding, bottomPadding, rightPadding, topPadding, id="leftColumn")
rightColumn = Frame((pagew/2), 0, framew, frameh, leftPadding, bottomPadding, rightPadding, topPadding, id="rightColumn")
styles = getSampleStyleSheet()
elements = []

def header(txt):
    s = Spacer(0.2*inch, 0.3*inch)
    elements.append(s)
    para = Paragraph(txt, styles["Heading1"])
    elements.append(para)

def p(txt):
    s = Spacer(0.2*inch, 0.1*inch)
    elements.append(s)
    para = Paragraph(txt, styles["Normal"])
    elements.append(para)

# NOT USED
# def pre(txt):
#     s = Spacer(0.1*inch, 0.1*inch)
#     elements.append(s)
#     p = Preformatted(txt, styles["Code"])
#     elements.append(p)

# Read the HTML and "perform" the h1 and p elements
with open("jekyll.html") as f:
    t = html5lib.parse(f, treebuilder="etree", namespaceHTMLElements=False)
    for elt in t.find(".//body"):
        if elt.tag == "h1":
            header(elt.text)
        elif elt.tag == "p":
            p(elt.text)

twoColumnPage = PageTemplate(id="base", frames=[leftColumn, rightColumn], pagesize=A4)
doc = BaseDocTemplate("jekyll.pdf", pageTemplates=[twoColumnPage])
# elements.insert(0,Spacer(0,inch))
# doc = SimpleDocTemplate('gfe.pdf')
doc.build(elements)

Result: jekyll.pdf

Example: SICV Random Walk

http://guttormsgaard.activearchives.org/cgi-bin/walk.cgi

RML

RMLL is a report-lab specific document markup language similar to HTML but much more limited (and specific to the capabilities of Report Lab). The "official" tools to process RML and produce PDFs are commercial, but there exist alternative open source implementations based on the open source report lab library.