PDF Template Engine and Editor
This project is an approach to separate the design of a PDF file from code instead of writing it completely in XOJO code. it consists of classes to render a PDF document from a text based template file (.ptf). The template defines what to be rendered into the PDF document at what position. Also a visual editor is included to create and edit PDF template files (.ptf).
I’ve started this project a couple of month ago because I needed to dynamically create Invoice documents. I found it hard to design a PDF document in code, because it required me to run the whole project each time I change anything to the document layout. Also I had to redeploy the whole project again after if I fixed a small typo or added a static text to the document. With this project I now can position all elements like static text, images or simple lines on a raster-based page and save the definition of all this elements to a simple text file (the .ptf template file). The template-file will then be used by my invoice app to generate the PDF invoice documents.
This project contains not a fully featured app and framework but it is useable for simple purposes and may be enhanced in the future as need. Currently one can design single page DIN A4 PDF templates using the visual editor and generate PDF documents using the TemplateEngine classes.
Project on github: GitHub - stefanwatermann/XojoPdfTemplateEngine: Classes to render PDF files from a text based template file and a visual editor to design PDF template files.
This is how the visual editor looks like:
And this is how to render the PDF from code and inject parameters that will replace the variables starting with %:
Private Function CreateCustomerInvoice(params as dictionary) As PDFDocument
Using PdfTemplate
Var pdfSvc As New PdfService
pdfSvc.Load(Self.PdfTemplateFolder.Child("customerinvoice.ptf"))
Var helper As New PdfHelper
helper.CreateA4PdfDoc
helper.DrawElements(pdfSvc.CanvasElements, params)
Return helper.PdfDoc
End Function
There are two projects in the repository, the first one is the visual editor (Desktop App) and the second one is an example Web-App to demonstrate how to use a template file to render a PDF document using the TemplateEngine classes. A sample template file can be found in the Web-App project (customerinvoice.pft).
