In the early days, we chose to delegate the responsibility of generating invoice PDFs to the client side. We simply designed the invoice in HTML and utilized the customer's browser for PDF generation. We adopted this approach because it was quick and straightforward to implement. However, it had its drawbacks. Primarily, it did not generate identical PDFs across all browsers as it heavily depended on the browser and its version. Secondly, we aimed to send invoices as PDFs via email, necessitating server-side PDF generation. Given that we already had an HTML template for the invoice, our initial thought was to convert HTML to PDF. There are multiple tools available for this, such as wkhtmltopdf. However, we hesitated to add another dependency as it could complicate our deployment process. We decided to use prawnpdf [^1] for server-side PDF generation. Prawn is a pure Ruby PDF generation library that offers a wide range of features for PDF generation. It is user-friendly and highly flexible. Prawn provides excellent manuals for reference [^2] [^3]. You can check the details and its functionalities. The design process was straightforward. The most significant challenge was dealing with encoding. Unfortunately, the default PDF fonts do not support all UTF-8 characters. If your text includes some special characters, it triggers the following error: Your document includes text that's not compatible with the Windows-1252 character set. (Prawn::Errors::IncompatibleStringEncoding) If you need full UTF-8 support, use external fonts instead of PDF's built-in fonts. As the exception suggests, we should use external fonts for full UTF-8 support. I considered several fonts, such as "OpenSans" and "DejaVuSans", before deciding on the "BeVietnamPro" [^4] font. It's already in use on our website and might be useful in our console at some point. Thus, it seemed sensible to use it for PDF generation as well. It's open-source and licensed under the SIL Open Font License [^5]. I added the "pdf-reader" gem to read the generated PDF file in the tests, which allows us to verify its content. [^1]: https://github.com/prawnpdf/prawn [^2]: https://prawnpdf.org/docs/prawn/2.5.0/manual.pdf [^3]: https://prawnpdf.org/prawn-table-manual.pdf [^4]: https://fonts.google.com/specimen/Be+Vietnam+Pro/about [^5]: https://openfontlicense.org
132 KiB
132 KiB