Does anyone know how to generate PDF from text programmatically for invoices?

I’m trying to generate PDF invoices from text in a small billing script, but the spacing keeps shifting. How can I keep totals aligned and handle invoices that run onto a second page?

To be fair, plain text coordinates always mess up spacing. The simplest thing is to use a basic HTML template with fixed table widths insted, then convert that to PDF. It handles page breaks and aligns totals automatically.