0% found this document useful (0 votes)
146 views1 page

Chapter 2 Graphics and Text With: Pdfgen

The document discusses the pdfgen package in Python, which provides a low-level interface for generating PDF documents. It describes how a pdfgen program works by "painting" instructions onto document pages using a canvas object. A simple example program is shown that creates a canvas, draws text onto it, displays the current page, and saves the file. Configuration options for the canvas constructor such as the filename, page size, and encoding are also outlined.

Uploaded by

Trogo Bo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
146 views1 page

Chapter 2 Graphics and Text With: Pdfgen

The document discusses the pdfgen package in Python, which provides a low-level interface for generating PDF documents. It describes how a pdfgen program works by "painting" instructions onto document pages using a canvas object. A simple example program is shown that creates a canvas, draws text onto it, displays the current page, and saves the file. Configuration options for the canvas constructor such as the filename, page size, and encoding are also outlined.

Uploaded by

Trogo Bo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

User Guide Chapter 2 Graphics and Text with pdfgen

Chapter 2 Graphics and Text with pdfgen

2.1 Basic Concepts


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 should be thought of as a sheet of white paper with points on the sheet identified using Cartesian
(X,Y) coordinates which by default have the (0,0) origin point at the lower left corner of the page.
Furthermore the first coordinate x goes to the right and the second coordinate y goes up, by default.
A simple example program that uses a canvas follows.

from reportlab.pdfgen import canvas


def hello(c):
c.drawString(100,100,"Hello World")
c = canvas.Canvas("hello.pdf")
hello(c)
c.showPage()
c.save()

The above code creates a canvas object which will generate a PDF file named hello.pdf in the current
working directory. It then calls the hello function passing the canvas as an argument. Finally the
showPage method saves the current page of the canvas and the save method stores the file and closes the
canvas.
The showPage method causes the canvas to stop drawing on the current page and any further operations
will draw on a subsequent page (if there are any further operations -- if not no new page is created). The
save method must be called after the construction of the document is complete -- it generates the PDF
document, which is the whole purpose of the canvas object.

2.2 More about the Canvas


Before describing the drawing operations, we will digress to cover some of the things which can be done to
configure a canvas. There are many different settings available. If you are new to Python or can't wait to
produce some output, you can skip ahead, but come back later and read this!
First of all, we will look at the constructor arguments for the canvas:

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

The filename argument controls the name of the final PDF file. You may also pass in any open file object
(such as sys.stdout, the python process standard output) and the PDF document will be written to that.
Since PDF is a binary format, you should take care when writing other stuff before or after it; you can't
deliver PDF documents inline in the middle of an HTML page!
The pagesize argument is a tuple of two numbers in points (1/72 of an inch). The canvas defaults to A4 (an
international standard page size which differs from the American standard page size of letter), but it is
better to explicitly specify it. Most common page sizes are found in the library module
reportlab.lib.pagesizes, so you can use expressions like

from reportlab.lib.pagesizes import letter, A4


myCanvas = Canvas('myfile.pdf', pagesize=letter)
width, height = letter #keep for later

NOTE

Page 10

You might also like