Better Programming

Advice for programmers.

Follow publication

Member-only story

How to Generate PDF Invoices With JavaScript

Trevor-Indrek Lasn
Better Programming
Published in
3 min readJun 15, 2017

--

JavaScript and PDF logos
Photo by the author.

Let’s say you wrote a shopping cart. You’re almost finished with the shopping cart, but there is one thing missing: sending out the purchase invoice! In most cases, the invoices are sent out in Portable Document Format (PDF).

Why PDF? Here’s a rather simple answer: PDF is easy to use and almost any device can read documents in that format.

Let’s get into it!

Assuming you know very very basics of JavaScript, let’s start by making our little project directory. Open your terminal and write this command:

$ mkdir js-to-pdf && cd js-to-pdf

Pretty self-explanatory. We make the project and navigate into the project folder.

Next up, let's make the index.html file:

$ touch index.html

With the help of the jsPDF package, we can generate PDFs from the client side.

Code
This is what we currently have.

Note: In a real-world scenario, we would use NPM to install our dependencies. This is a lesson focused on the jsPDF library.

--

--

Write a response