Skip to content

Open In Colab


Open In nbviewer

Hello World with fpdf2

This Jupyter notebook demontrates some basic usage of the Python fpdf2 library

# Installation of fpdf2 with PIP:
!pip install fpdf2

# Enable deprecation warnings:
import warnings
warnings.simplefilter('default', DeprecationWarning)
# Generate a PDF:
from fpdf import FPDF
pdf = FPDF()
pdf.add_page()
pdf.set_font('helvetica', size=48)
pdf.cell(text="hello world")
pdf_bytes = pdf.output()
# Display the PDF in the notebook by embedding it as HTML content:
WIDTH, HEIGHT = 800, 400 
from base64 import b64encode
from IPython.display import display, HTML
base64_pdf = b64encode(pdf_bytes).decode("utf-8")
display(HTML(f'<embed height="{HEIGHT}" src="data:application/pdf;base64,{base64_pdf}" type="application/pdf" width="{WIDTH}"/>'))
# Diplay a download button:
display(HTML(f'<a download="fpdf2-demo.pdf" href="data:application/pdf;base64,{base64_pdf}">Click to download PDF</a>'))

To continue learning about fpdf2, check our tutorial: - English - Deutsch - español - हिंदी - português - Русский - Italian - français - Ελληνικά - עברית - Dutch