Outils pour utilisateurs

Outils du site


issue91:python

Ceci est une ancienne révision du document !


Table des matières

1

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’)

Générateur de modèle de point de croix - Partie 4 - Comprendre pyfPDF

Désolé d'avoir manqué tant de mois. Je ne peux toujours pas rester assis pendant trop longtemps, du coup cet article est plus court que d'habitude. Mon plan initial était de passer directement à la partie du programme qui crée le PDF, mais il y a tellement de choses à comprendre dans cette bibliothèque que j'ai décidé d'utiliser cet épisode comme un tutoriel sur pyfPDF et attaquer la sortie PDF la prochaine fois. Donc, nous allons commencer.

FPDF signifie PDF gratuit. Voici un exemple très minimaliste :

from fpdf import FPDF

pdf = FPDF()

pdf.add_page()

pdf.set_font(‘Arial’,’B’,16)

pdf.cell(40,10,’Bonjour de Python’)

pdf.output(‘exemple1.pdf’,’F’)

2

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)

La première ligne importe la bibliothèque. La suivante crée une instance de l'objet FPDF. Nous utilisons pour cet exemple les valeurs par défaut, qui sont : • Portrait • Unité de mesure = millimètres • Format = A4

Si vous avez besoin d'utiliser les normes « US», vous pouvez le faire de cette façon :

pdf=FPDF(‘P’,’in’,’Letter’)

3

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.

Notez que les paramètres sont FPDF(orientation, unités, format) : • les valeurs possibles pour l'orientation sont « P » pour portrait et « L » pour paysage (« landscape »). • les valeurs possibles pour les unités sont : 'pt' (points), 'mm' (millimètre), ‘cm’ (centimètre), 'in' (pouces). • les valeurs possibles pour le format sont : ‘A3’, ‘A4’, ‘A5’, ‘Letter’, ‘Legal’ ou un tuple contenant la largeur et la hauteur exprimées dans l'unité donnée dans le paramètre précédent.

La troisième ligne crée une page pour écrire des données. Remarquez qu'une page n'est pas automatiquement créée lorsque nous créons l'instance de l'objet. L'origine de la page est le coin supérieur gauche, et la position de départ se situe par défaut à 1 cm des marges. Les marges peuvent être modifiées avec la fonction SetMargins.

4

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)

5

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().

6

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=“”)

7

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)

8

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.

9

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.

issue91/python.1420658576.txt.gz · Dernière modification : 2015/01/07 20:22 de fredphil91