Simple tables with node and pdfkit
I was working on a report creation feature yesterday, using node, express and pdfkit. Eventually I had to generate a bunch of tables so I started looking for node modules that would allow me to insert tables in a pdf document. Surprisingly, I found very little: the only two alternatives were Voilab pdf tables and pdfmake. The latter is an advanced declarative library to generate pdf documents client-side in the browser; considering that I was bound to use pdfkit with its imperative syntax and the fact that the approach was completely different and required several despicable workaround, I ultimately decided to walk away from pdfmake. This only left me with the first option, which is quite nice. However, as an experiment, I wanted to check if I could render some simple tables through low level pdfkit rendering calls. It turned out to be quite simple and effective, so why not sharing it?
1 | ; |
I wrote the code to make it extend the original PDFDocument implementation so that, from outside, it looks like tables were natively implemented. I also used the same signature styles the author chose for all the rendering functions. With the following simple example you can see that it is really easy to obtain a neat-looking result despite the lack of customisation:
1 | ; |
Which produces this result.
I didn’t publish this code because of course it is extremely trivial and does not allow for the rich customisation experience that other libraries and frameworks offer. Nonetheless it was a nice experiment with a neat acceptable result! I hope that someone will find it useful eventually.