Typing Stubs for fpdf2
This is a type stub package for the fpdf2 package, providing external type annotations for static analysis and type checking. It is part of the `typeshed` project and receives regular, often daily, automated releases to track changes in the upstream `fpdf2` library. This version of `types-fpdf2` aims to provide accurate annotations for `fpdf2==2.8.4`. Note that since `fpdf2` version 2.8.6, the `fpdf2` package itself includes type annotations, rendering `types-fpdf2` unnecessary for newer `fpdf2` versions.
Warnings
- gotcha If you are using `fpdf2` version 2.8.6 or newer, `types-fpdf2` is not needed as `fpdf2` itself provides type annotations. Installing both may lead to redundant or conflicting type information, potentially causing unexpected type checking errors.
- gotcha `types-fpdf2` provides type hints for `fpdf2`'s font methods, but cannot guarantee that the fonts themselves support specific Unicode characters at runtime. If you're working with non-Latin characters, ensure you explicitly load a Unicode font with `pdf.add_font()` and set it with `pdf.set_font()` to avoid runtime `UnicodeEncodeError` exceptions.
- gotcha While `types-fpdf2` provides type hints for text rendering functions, `fpdf2` itself has known limitations with right-to-left (RTL) languages, particularly concerning automatic line breaking (`multi_cell`). Text direction and justification may not be handled correctly for all scripts.
Install
-
pip install types-fpdf2
Imports
- FPDF
from fpdf import FPDF
Quickstart
from fpdf import FPDF
def create_pdf_document(filename: str = "hello_world.pdf") -> None:
pdf: FPDF = FPDF()
pdf.add_page()
pdf.set_font("Helvetica", size=12)
pdf.cell(200, 10, txt="Hello, world!", align="C")
pdf.output(filename)
print(f"PDF saved to {filename}")
if __name__ == "__main__":
create_pdf_document()