Ceci est une ancienne révision du document !
Cross Stitch Pattern Generator - Part 4 - Understanding pyfPDF
Sorry for missing so many months. I still can’t sit for long periods of time, so this article might be shorter than what you are used to. My original plan was to jump right into the PDF output portion of the program, but there is so much to understand about this library, I decided to use this installment as a tutorial on pyfPDF and then tackle the PDF output next time. So let’s get started.
FPDF stands for Free PDF. A VERY minimal example would be as follows:
from fpdf import FPDF
pdf = FPDF()
pdf.add_page()
pdf.set_font(‘Arial’,’B’,16)
pdf.cell(40,10,’Hello From Python’)
pdf.output(‘example1.pdf’,’F’)
The first line imports the library file. The next creates an instance of the FPDF object. We use the default values for this example, which are: • Portrait • Measure Unit = Millimeters. • Format = A4
If you need to use ‘US’ standards, you could do it this way:
pdf=FPDF(‘P’,’in’,’Letter)
Notice the parameters are FPDF(orientation, units, format): • Possible values for orientation are “P” for Portrait and “L” for Landscape. • Possible values for units are: ‘pt’ (poiints), ‘mm’ (millimeter), ‘cm’ (centimeter), ‘in’ (inches). • Possible values for format are: ‘A3’, ‘A4’, ‘A5’, ‘Letter’, ‘Legal’ or a tuple containing the width and height expressed in the unit given in the unit parameter.
The third line creates a page to enter data into. Notice a page is not automatically created when we create the instance of the object. The origin of the page is the upper-left corner, and the current position defaults to 1 cm from the margin. The margin can be changed with the SetMargins function.
Before you can actually print any text, you must call pdf.set_font() to define a font. In the line above, we are defining Arial Bold 16 point. Standard valid fonts are Arial, Times, Courier, Symbol and ZapfDingbats.
Now we can print a cell with the pdf.cell() call. A cell is a rectangular area, possibly framed, which contains some text. Output is at the current position which is specified (40,10 cm) in the above example. The parameters are:
pdf.cell(Width, Height, text, border, line, align, fill, link)
Where: • Width is length of cell. If 0, width extends to the right margin. • Height is the height of the cell. • Text is the string of text you want to print. • Border is either 0 (no border(default)), 1 is border, or a string of any or all of the following characters: “L”,“T”,“B”,“R” • Line is where the current position should go after printing the text. Values are 0 (to the right), 1 (to the beginning of the next line, 2 (below). Default is 0, and putting 1 is equivalent to putting 0 and calling ln() immediatly after. • Align allows to center or align the text within the cell. Values are “L” (left), “C” (center), “R” (right). • Fill sets the background to be painted (true) or transparent (false). Default is false. • Link is a url or identifier returned by addlink().
Finally, the document is closed and sent to the file with Output. The parameters are fpdf.output(name,dest). If file is not specified, the output will be sent to the browser. Options for destination are “I” (inline to browser(default)), “F” (local file given by name), “D” (to the browser and force a file download with the name passed), and “S” (return the document as a string).
Since we will be sending our cross stitch images to the pdf file, we will have to understand the image function.
The function is called like this:
pdf.image(name,x=None,y=None,w=0,h=0,type=“”,link=“”)
This function puts the image. The size it will take on the page can be specified in different ways: • Explicit width and height or • One explicit dimension
Supported formats are JPEG, PNG, and GIF. If you wish to use GIF files, you must get the GD extension.
For JPEGs, all flavors are allowed: • gray scale • true colours (24 bits) • CMYK (32 bits)
For PNGs, the following are allowed: • gray scales on at most 8 bits (256 levels) • indexed colors • true colors (24 bits)
Note: interlacing is not allowed, and if you are using a version of FPDF prior to 1.7, Alpha channel is not supported.
I stole this example (shown right) from the pyFPDF tutorial.
You have been around long enough that you should be able to look at the program and understand what is going on. But in this example the line we are REALLY interested in is the fourth line:
this.image('img1.png',10,8,33)
In this instance, we are calling the image function with the filename, the x position of where the picture will go on the page, the y position, and the width of the picture.
Now that you have a gross grasp of the library, we will start our PDF code next time.
Until then, have a good month. See you soon.