Last week I shared about my invoicing system that I had built. The process in generating the invoice went like this. I’d first create a TOML file with the necessary information for the invoice, then I’d run it through my Node invoicing script which then would generate an HTML file. Finally I would open the HTML file in Google Chrome and save as a PDF to then be sent off to a client.
While the initial setup was pretty nice, it wasn’t slick. Meaning it was too hands on and manual towards the last steps of taking the HTML and generating a PDF. As mentioned in my last article about my invoicing system, I knew adding a step to handle the HTML to PDF conversion would enhance the workflow.
I had previously heard of PhantomJS being used for things like capturing screenshots and rendering websites for automated testing. All without actually opening up an actual browser. Doing a quick search, it turns out that there is a Node package that takes HTML and converts it into a PDF using PhantomJS. This was perfect because my script already generated HTML, now I can add the package in there and get a PDF while shaving a few steps off the PDF generation.
Adding in the package wasn’t too much of a challenge. It was actually tweaking the options passed into the package to get a decent PDF output which took some time to tweak. But now that everything is wired in, it truly feels like magic. I input TOML and out comes a PDF invoice.
So as of right now the script does this to generate the PDF.
TOML -> Parsed to JSON -> Passed into Handlebars template -> HTML -> PDF
All generated from
node invoice.js ~/invoice.toml ~/invoice.pdf. Where
~/invoice.toml is the input of the TOML file and
~/invoice.pdf is the output. I will most likely update the script to automatically output the PDF in the same directory as the input and replace .toml with .pdf to further simplify the script. Adding in PhantomJS really made the script feel magical. Definitely nerded out when I first ran it.