Hello World with fpdf2¶
This Jupyter notebook demontrates some basic usage of the Python fpdf2 library
In [1]:
# Installation of fpdf2 with PIP:
!pip install fpdf2
In [1]:
# Enable deprecation warnings:
import warnings
warnings.simplefilter('default', DeprecationWarning)
In [1]:
# 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()
In [14]:
# 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 src="data:application/pdf;base64,{base64_pdf}" width="{WIDTH}" height="{HEIGHT}" type="application/pdf">'))
In [1]:
# Display a download button:
display(HTML(f'<a download="fpdf2-demo.pdf" href="data:application/pdf;base64,{base64_pdf}">Click to download PDF</a>'))