- fpdf2
from js import document
from fpdf import FPDF, HTMLMixin
import base64
class PDF(FPDF, HTMLMixin):
def header(self):
# Rendering logo:
# Setting font: helvetica bold 15
self.set_font("helvetica", "B", 15)
# Moving cursor to the right:
self.cell(40)
# Printing title:
self.cell(80, 10, "PyScript FPDF2 Demo", border=1, align="C")
# Performing a line break:
self.ln(20)
def footer(self):
# Position cursor at 1.5 cm from bottom:
self.set_y(-15)
# Setting font: helvetica italic 8
self.set_font("helvetica", "I", 8)
# Printing page number:
self.cell(0, 10, f"Page {self.page_no()}/{{nb}}", align="C")
pdf = PDF()
pdf.add_page()
pdf.set_font('helvetica', size=12)
pdf.cell(txt="Now is the time for all great developers to learn PyScript!")
pdf.ln(10)
pdf.cell(txt="Learning PyScript is easy if you read my articles:")
pdf.cell(txt="PyScript Archive", border=0, align="C", link="https://www.jhanley.com/category/pyscript/")
# Get the PDF and Base64 encode
b64data = base64.b64encode(pdf.output())
# Create an iframe to display the PDF
iframe = document.createElement('iframe');
iframe.width = '800'
iframe.height = '600'
iframe.style = 'border: 0'
iframe.src = 'data:application/pdf;base64,' + b64data.decode()
# Append the iframe to the HTML body
document.body.appendChild(iframe);