First book

First book#

We’ll start using Python to program books. For that we’ll work with the library fpdf2

We’ll create our first pdf with the example code from the website:

from fpdf import FPDF

pdf = FPDF()
pdf.add_page()
pdf.set_font('helvetica', size=12)
pdf.cell(text="hello world")
pdf.output("hello_world.pdf")

001.png

Next we’ll change the page size and font size.

# Import the FPDF class from the fpdf library
from fpdf import FPDF

# Create a FPDF object, 
# store it in the variable called pdf 
# and define width and height of the document
pdf = FPDF(format=(115, 180))  # width and height are inserted as a tuple data type: (w, h)

# Add a new page to the FPDF object
pdf.add_page()

# Set the font and font size
pdf.set_font('helvetica', size=48)

# Write "hello world" into a text cell
pdf.cell(text="hello world")

# Export the pdf under the name "hello_world.pdf" in a folder named "pdfs"
pdf.output("pdfs/hello_world.pdf")

002.png