fpdf2 is a simple and fast Python library for generating PDF documents, actively maintained as a modern fork of the original FPDF. It supports a wide range of features including text, images, shapes, tables, and SVG rendering. The current version is 2.8.7, and it has a rapid release cadence with frequent updates adding new features and bug fixes.
pip install fpdf2Verified import paths — ran on the pinned version, not inferred.
This quickstart code generates a simple PDF document named 'hello_world.pdf' containing the text 'Hello, World!' using the FPDF class.
Upgrade Python to 3.9 or newer. Alternatively, if Python 3.8 must be used, restrict fpdf2 to `<2.8.4` (e.g., `pip install 'fpdf2<2.8.4'`).
Always install with `pip install fpdf2` and ensure your code uses `from fpdf import FPDF`. Refer to the official `fpdf2` documentation for current API details.
Specify the desired unit in the `FPDF` constructor (e.g., `pdf = FPDF(unit='pt')` or `pdf = FPDF(unit='in')`). All subsequent measurements will use the chosen unit. You can also convert units manually if needed.
Update your code to use `text` instead of `txt` for parameters in affected methods (e.g., `pdf.cell(text="Hello, World!")`).
Update calls to `pdf.cell()` (and potentially other methods) to use `text=` instead of `txt=` (e.g., `pdf.cell(text="Hello, World!")`). This change was introduced in `fpdf2` version 2.7.6.
Ensure only `fpdf2` is installed and the correct package is being used. First, uninstall any conflicting `fpdf` package: `pip uninstall fpdf`. Then, if `fpdf2` is not already installed, install it: `pip install fpdf2`. The import statement `from fpdf import FPDF` is correct for `fpdf2`.
Upgrade `fpdf2` to the latest version, as table support was added in a newer release. Run `pip install --upgrade fpdf2`. If the original `fpdf` package is also installed, uninstall it first using `pip uninstall fpdf` to prevent conflicts.
Add and use a Unicode (TrueType) font that supports the required characters. First, download a suitable `.ttf` font (e.g., from Google Fonts). Then, add it to `fpdf2` and set it as the current font: `pdf.add_font('DejaVuSans', fname='DejaVuSans.ttf')` followed by `pdf.set_font('DejaVuSans')`.Explicitly convert the numerical data to a string before passing it to `fpdf2` text methods: `str(numpy_int_variable)`. For example, `pdf.cell(0, 10, str(value))` instead of `pdf.cell(0, 10, value)`.