Awesome
Extensions / Scripts for pyFPDF (fpdf2)
This repository contains some of the original FPDF PHP Scripts that were ported to Python to be used in conjunction with fpdf2 (as well as some new scripts).
My main goal was on translating the scripts 1:1 without a focus on "pythonic elegance" or optimizations.
Some of the functions demonstrated in these scripts have already found their way into fpdf2. Whenever possible, you should use the alternatives implemented in fpdf2. On the other hand, some functions in the scripts may work (slightly) different and add value to your projects or adress edge cases.
It is possible that the scripts do not work correctly with newer versions of fpdf2 or Python! Please check the "Tested with" info for each script.
Scripts
- Alpha / Transparency -> Code -> This is now available in fpdf2
- Masking images / Alpha channel -> Code
- 360 Degree Text -> Code
- Transform & Circular Text -> Code
- Diagrams -> Code
- Javascript support -> Code
- Rectangles with rounded corners -> Code -> This is now available in fpdf2
- Draw segments of circles -> Code
- Draw star shapes -> Code -> This is now available in fpdf2
- Restrict elements to be displayed on screen / printed only -> Code
- Gradients -> Code
- Stamp and Watermark -> Code
- Adjust text to cell width -> Code
- Skew, shear, rotate text -> Code
- Sinus (Sine) Text -> Code
Alpha / Transparency for pyFPDF (fpdf2)
This script adds transparency support to pyFPDF. You can set the alpha channel from 0 (fully transparent) to 1 (fully opaque). It applies to all elements (text, drawings, images).
Tested with fpdf2 2.4.5, Pillow 8.3.1, Python 3.8.10
OBSOLETE! This is now available in fpdf2
Example
Get the code
Masking images / Alpha channel
This script exposes the feature to add transpareny to an image by supplying a separate PNG image (called a soft mask).
Tested with fpdf2 2.4.5, Pillow 8.3.1, Python 3.8.10
Example
Get the code
360 Degree Text
This script adds 360 degree text. B
, U
and I
are supported.
Tested with fpdf2 2.4.5, Pillow 8.3.1, Python 3.8.10
Example
Get the code
Transform & Circular Text
This script adds transfromation featurea ans support for circular text to pyFPDF.
Tested with fpdf2 2.4.5, Pillow 8.3.1, Python 3.8.10
Example
Get the code
Diagrams
Draw pie charts and bar diagrams with this script.
Tested with fpdf2 2.4.5, Pillow 8.3.1, Python 3.8.10
Example
Get the code
Embed Javascript in PDF
This script allows to embed JavaScript inside the PDF. The code is executed when the document is opened.
The Acrobat JavaScript reference is available here.
Note: Support depends on the PDF viewer and security settings.
Tested with fpdf2 2.4.5, Pillow 8.3.1, Python 3.8.10
Example
Get the code
Rectangle with rounded corners
Draw rectangles with rounded corners
Tested with fpdf2 2.4.5, Pillow 8.3.1, Python 3.8.10
OBSOLETE! This is now available in fpdf2
Example
Get the code
Sectors of circle shapes
This script allows to draw segments of a circle. It can be used to render a pie chart.
Tested with fpdf2 2.4.5, Pillow 8.3.1, Python 3.8.10
Example
Get the code
Star shape
This script draws a star.
Tested with fpdf2 2.4.5, Pillow 8.3.1, Python 3.8.10
OBSOLETE! This is now available in fpdf2
Example
Get the code
Visibility feature
This script allows to restrict the rendering of some elements to screen or printout. This can be useful, for instance, to put a background image or color that will show on screen but won't print.
Note: this feature is not supported by all PDF viewers.
Some Test on Ubuntu 20.04:
- Chromium 94.0.4606.81 (Works as intended)
- Google Chrome 94.0.4606.81 (Works as intended)
- Evince 3.36.10 (Does not display content marked for screen display, but displays content marked for printing)
- Okular 21.08.2 (Same result as for Evince)
- Firefox 93.0 (Ignores restrictions and displays all content)
Script was tested with fpdf2 2.4.5, Pillow 8.3.1, Python 3.8.10
Get the code
Linear, radial and multi-color gradients
Paints linear and radial gradients as well as multi-color gradients (coons patch meshes) inside a rectangle.
Tested with fpdf2 2.4.5, Pillow 8.3.1, Python 3.8.10
Example
Get the code
Stamp and Watermark PDF files
This script adds support for stamps (overlay) and watermarks (underlay) to PDF files by modifying the 'header()' and 'footer()' functions.
Tested with fpdf2 2.4.5, Pillow 8.3.1, Python 3.8.10
Example
Get the code
Adjust text to cell width
This method is an extension of cell() allowing to output text with either character spacing or horizontal scaling.
Tested with fpdf2 2.4.5, Pillow 8.3.1, Python 3.8.10
Example
Get the code
Skew, shear and rotate text
This extension allows to print rotated and sheared (i.e. distorted like in italic) text.
Tested with fpdf2 2.4.5, Pillow 8.3.1, Python 3.8.10
Example
Get the code
Sinus (Sine) Text
This script adds wiggly text :)
Tested with fpdf2 2.4.5, Pillow 8.3.1, Python 3.8.10