You are browsing as a guest. Sign up (or log in) to start making projects!

8h 37m 18s logged

Hey guys, how’s it going? I hope you’re all doing well.
School started here in Italy, so I have less time compared to August. I also have to get my driver’s license, so I’m studying for that too. Unfortunately, coding isn’t the first thing I do every day anymore.
I’m still working on the PDF generator.
Last time I mentioned that relation fields were showing raw IDs and that document totals weren’t formatted properly. I fixed both of those, but I’ll get back to them later.

About the PDF

The template is HTML, and it lives in the DB.
I didn’t hardcode the layout in Java. I created a PdfTemplate entity that stores the HTML, page size and margins. There is one default template for each document type: quote, order, delivery note and invoice.
Thymeleaf fills the template with the document data, and OpenHTMLToPDF + PDFBox generate the PDF.
Since the HTML is provided by the user, I also had to think about security. A template could try to load something like file:///etc/passwd or access an internal URL.
Because of this, external protocols are disabled and only data: URIs are allowed. Page sizes are also checked against a whitelist.
I also had to deal with the fact that HTML isn’t always valid XML. OpenHTMLToPDF expects XML, so I use jsoup to normalize the HTML first. Otherwise, perfectly normal HTML can cause a SAXParseException.
And finally, fonts have to be embedded in the PDF. Without including DejaVu Sans in the jar, the PDF doesn’t render correctly.

VAT summary

I also worked on the VAT summary.
Lines are grouped by VAT rate, shipping is added to the highest rate, and the document discount is distributed across the rates instead of being applied at the end.
VAT is then calculated on the discounted amounts, so everything adds up correctly:
taxable + VAT = total
This actually fixed a bug where VAT was calculated before applying the document discount. It wasn’t very obvious until I started testing it with real numbers.

Three endpoints

The PDF system now has three endpoints:

  • print a document
  • preview a template
  • print a batch
    For the preview, instead of creating a fake document, I use the last real document of that module. This way there is only one PDF generation system to maintain.
    The batch endpoint combines multiple documents into one PDF, while checking permissions for every document.

A few other fixes

The two ugly things from last time are also gone.
Relation titles are now resolved by a shared backend service, so instead of UUIDs you see things like the company name. The same service is used by the lists, detail pages and PDFs.
Amounts in the PDF are also formatted using the user’s preferences.
I also added tags to the record lists. Tags are resolved in bulk instead of doing a permission check for every single row, which makes a big difference on larger lists.

The backend part is basically done for this cycle.
The print button and template editor are already written, but they’re not wired up yet. Right now I’m still using the API to test the generator.
So, UI is next.
See you in the next devlog!

0
13

Comments 0

No comments yet. Be the first!