issue91:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue91:python [2015/01/02 15:56] – créée andre_domenech | issue91:python [2015/02/25 10:01] (Version actuelle) – [9] auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Cross Stitch Pattern Generator - Part 4 - Understanding pyfPDF | + | ====== 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. | 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. | ||
Ligne 15: | Ligne 17: | ||
pdf.cell(40, | pdf.cell(40, | ||
- | pdf.output(‘example1.pdf’, | + | pdf.output(‘example1.pdf’, |
- | 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: | + | Générateur de modèle de point de croix - Partie 4 - Comprendre pyfPDF |
+ | |||
+ | Désolé d' | ||
+ | |||
+ | FPDF signifie PDF gratuit. Voici un exemple très minimaliste : | ||
+ | |||
+ | from fpdf import FPDF | ||
+ | |||
+ | pdf = FPDF() | ||
+ | |||
+ | pdf.add_page() | ||
+ | |||
+ | pdf.set_font(‘Arial’, | ||
+ | |||
+ | pdf.cell(40, | ||
+ | |||
+ | pdf.output(‘exemple1.pdf’, | ||
+ | |||
+ | ====== 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 | • Portrait | ||
• Measure Unit = Millimeters. | • Measure Unit = Millimeters. | ||
Ligne 24: | Ligne 46: | ||
If you need to use ‘US’ standards, you could do it this way: | If you need to use ‘US’ standards, you could do it this way: | ||
- | pdf=FPDF(‘P’, | + | pdf=FPDF(‘P’, |
- | Notice the parameters are FPDF(orientation, | + | La première ligne importe la bibliothèque. La suivante crée une instance de l' |
+ | • Portrait | ||
+ | • Unité de mesure = millimètres | ||
+ | • Format = A4 | ||
+ | |||
+ | Si vous avez besoin d' | ||
+ | |||
+ | pdf=FPDF(‘P’, | ||
+ | |||
+ | ====== 3 ====== | ||
+ | |||
+ | **Notice the parameters are FPDF(orientation, | ||
• Possible values for orientation are “P” for Portrait and “L” for Landscape. | • Possible values for orientation are “P” for Portrait and “L” for Landscape. | ||
• Possible values for units are: ‘pt’ (poiints), ‘mm’ (millimeter), | • Possible values for units are: ‘pt’ (poiints), ‘mm’ (millimeter), | ||
• Possible values for format are: ‘A3’, ‘A4’, ‘A5’, ‘Letter’, | • Possible values for format are: ‘A3’, ‘A4’, ‘A5’, ‘Letter’, | ||
- | 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. | + | 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. | + | Notez que les paramètres sont FPDF (orientation, |
+ | • Les valeurs possibles pour l' | ||
+ | • Les valeurs possibles pour les unités sont : ' | ||
+ | • Les valeurs possibles pour le format sont : ‘A3’, ‘A4’, ‘A5’, ‘Letter’, | ||
+ | |||
+ | 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' | ||
+ | |||
+ | ====== 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: | 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, | + | pdf.cell(Width, |
+ | |||
+ | Avant de pouvoir afficher du texte, vous devez appeler pdf.set_font() pour définir une police. Dans la ligne ci-dessus, nous définissons Arial Bold 16 points. Les polices standards valides sont Arial, Times, Courier, Symbol et ZapfDingbats. | ||
+ | |||
+ | Maintenant, nous pouvons imprimer une cellule en appelant pdf.cell(). Une cellule est une zone rectangulaire, | ||
+ | |||
+ | pdf.cell(largeur, | ||
+ | |||
+ | ====== 5 ====== | ||
- | Where: | + | **Where: |
• Width is length of cell. If 0, width extends to the right margin. | • Width is length of cell. If 0, width extends to the right margin. | ||
• Height is the height of the cell. | • Height is the height of the cell. | ||
Ligne 47: | Ligne 97: | ||
• Align allows to center or align the text within the cell. Values are " | • Align allows to center or align the text within the cell. Values are " | ||
• Fill sets the background to be painted (true) or transparent (false). Default is false. | • Fill sets the background to be painted (true) or transparent (false). Default is false. | ||
- | • Link is a url or identifier returned by addlink(). | + | • 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, | + | où : |
+ | • largeur est la largeur de la cellule. Si égale à 0, la largeur va jusqu' | ||
+ | • hauteur est la hauteur de la cellule ; | ||
+ | • texte est la chaîne de texte que vous souhaitez afficher ; | ||
+ | • bordure est soit 0 (pas de bordure, par défaut), 1 pour une bordure, ou une chaîne de tout ou partie des caractères suivants: « L», « T », « B », « R » ; | ||
+ | • ligne indique la position à laquelle on doit aller après l' | ||
+ | • alignement permet de centrer ou aligner le texte dans la cellule. Les valeurs sont « L » (gauche), « C » (centre), « R » (droite) ; | ||
+ | • remplissage définit si le fond est rempli (true) ou transparent (false). Par défaut c'est false. | ||
+ | • Lien est une url ou un identifiant retourné par addlink(). | ||
+ | |||
+ | ====== 6 ====== | ||
+ | |||
+ | **Finally, the document is closed and sent to the file with Output. The parameters are fpdf.output(name, | ||
Since we will be sending our cross stitch images to the pdf file, we will have to understand the image function. | Since we will be sending our cross stitch images to the pdf file, we will have to understand the image function. | ||
Ligne 55: | Ligne 117: | ||
The function is called like this: | The function is called like this: | ||
- | pdf.image(name, | + | pdf.image(name, |
- | + | ||
- | This function puts the image. The size it will take on the page can be specified in different ways: | + | Enfin, le document est fermé et envoyé au fichier avec Output. Les paramètres sont fpdf.output(nom, |
+ | |||
+ | Puisque nous allons envoyer nos images de point de croix dans le fichier pdf, nous devrons comprendre la fonction image. | ||
+ | |||
+ | Cette fonction est appelée comme ceci : | ||
+ | |||
+ | pdf.image(nom, | ||
+ | |||
+ | ====== 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 | • Explicit width and height or | ||
• One explicit dimension | • One explicit dimension | ||
Ligne 66: | Ligne 138: | ||
• gray scale | • gray scale | ||
• true colours (24 bits) | • true colours (24 bits) | ||
- | • CMYK (32 bits) | + | • CMYK (32 bits)** |
- | For PNGs, the following are allowed: | + | Cette fonction place une image. La taille qu' |
+ | • la largeur et la hauteur explicite ou | ||
+ | • une dimension explicite. | ||
+ | |||
+ | Les formats supportés sont JPEG, PNG et GIF. Si vous souhaitez utiliser des fichiers GIF, vous devez installer l' | ||
+ | |||
+ | Pour les fichiers JPEG, tous les types sont autorisés : | ||
+ | • échelle de gris ; | ||
+ | • couleurs vraies (24 bits) ; | ||
+ | • CMYK (32 bits). | ||
+ | |||
+ | ====== 8 ====== | ||
+ | |||
+ | **For PNGs, the following are allowed: | ||
• gray scales on at most 8 bits (256 levels) | • gray scales on at most 8 bits (256 levels) | ||
• indexed colors | • indexed colors | ||
Ligne 75: | Ligne 160: | ||
Note: interlacing is not allowed, and if you are using a version of FPDF prior to 1.7, Alpha channel is not supported. | 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. | + | 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: | + | Pour PNG, les types suivants sont acceptés : |
+ | • niveaux de gris sur au plus 8 bits (256 niveaux) ; | ||
+ | • couleurs indexées ; | ||
+ | • couleurs vraies (24 bits). | ||
+ | |||
+ | Remarque : l' | ||
+ | |||
+ | J'ai volé cet exemple (à droite) dans le tutoriel de pyFPDF. | ||
+ | |||
+ | ====== 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(' | this.image(' | ||
Ligne 85: | Ligne 181: | ||
Now that you have a gross grasp of the library, we will start our PDF code next time. | 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. | + | Until then, have a good month. See you soon.** |
+ | |||
+ | Vous avez vu assez de choses pour être en mesure d' | ||
+ | |||
+ | this.image(' | ||
+ | |||
+ | Dans ce cas, nous appelons la fonction image avec le nom du fichier, la position x de l' | ||
+ | |||
+ | Maintenant que vous avez une connaissance grossière de la bibliothèque, | ||
+ | |||
+ | Jusque-là, passez un bon mois. À bientôt. | ||
+ | |||
+ | ====== Cadre orangé page 12 ====== | ||
+ | |||
+ | from fpdf import FPDF | ||
+ | class PDF(FPDF): | ||
+ | def header(this): | ||
+ | # Logo - remplacer par un petit PNG de votre choix | ||
+ | this.image(' | ||
+ | # Arial bold 15 | ||
+ | this.set_font(' | ||
+ | # se deplacer vers la droite | ||
+ | this.cell(80) | ||
+ | # titre | ||
+ | this.cell(30, | ||
+ | # saut de ligne | ||
+ | this.ln(20) | ||
+ | # instanciation de la classe | ||
+ | pdf=PDF() | ||
+ | pdf.alias_nb_pages() | ||
+ | pdf.add_page() | ||
+ | pdf.set_font(' | ||
+ | for i in range(1, | ||
+ | pdf.cell(0, | ||
+ | pdf.output(' |
issue91/python.1420210574.txt.gz · Dernière modification : 2015/01/02 15:56 de andre_domenech