SØAD includes built-in support for PDF generation using Flying Saucer. This recipe demonstrates how to render an HTML view as a styled PDF document—ideal for invoices, reports, or certificates.
fromutilsimportrenderclassInvoice(object):defview(self,ctx):# Set values to be rendered inside the HTML viewctx.output["invoice_no"]="INV-2024-005"ctx.output["customer_name"]="Ahmad Bin Ali"ctx.output["items"]=[{"desc":"Product A","qty":2,"price":100},{"desc":"Product B","qty":1,"price":250}]grand_total=0foriteminctx.output["items"]:total_price=item.get("qty")*item.get("price")item["total_price"]=total_pricegrand_total=grand_total+total_pricectx.output["total"]=grand_total# Generate the PDF from the HTML viewctx.go_to=render.as_pdf(ctx,"invoice",attachment=False)
While SØAD provides built-in PDF rendering via Flying Saucer for HTML-to-PDF conversion, you can also use OpenPDF for programmatic PDF generation—ideal for simple layout control, table drawing, or dynamic document building without relying on HTML.
fromutilsimportrenderfromcom.lowagie.textimportDocument,Paragraph,Font,FontFactoryfromcom.lowagie.text.pdfimportPdfWriterfromjava.ioimportByteArrayOutputStreamclassInvoice(object):defpdf(self,ctx):output_stream=ByteArrayOutputStream()document=Document()PdfWriter.getInstance(document,output_stream)document.open()title_font=FontFactory.getFont(FontFactory.HELVETICA_BOLD,16)normal_font=FontFactory.getFont(FontFactory.HELVETICA,12)document.add(Paragraph("Invoice Summary",title_font))document.add(Paragraph("Customer: Ahmad Bin Ali",normal_font))document.add(Paragraph("Invoice #: INV-2024-005",normal_font))document.add(Paragraph("Total: RM450",normal_font))document.close()ctx.go_to=render.as_blob(ctx,output_stream.toByteArray(),"application/pdf","invoice.pdf",attachment=False)
View the PDF
The user can open this transaction via:
/t/example/invoice/pdf
The browser open the rendered PDF content.
Do you know?
Use attachment=True to force download instead of displaying inline.
For simple HTML-to-PDF, prefer Flying Saucer for better styling support.