The JS runtimes
Published in

The JS runtimes

Generate PDF using pdfkit in Deno

In this article, we’ll go over generating PDFs in Deno. Some popular ways to generate PDF in Node or Deno: 1) pdfkit package 2) pdf-lib package. Both of these are popular, but pdfkit has more downloads from NPM. We’ll see how to use the pdfkit module to generate server-side PDFs in Deno.

Imports

The first step to get the required imports. Only two imports are required. With full NPM support, any NPM package can be imported using the NPM specifier (npm:<package-name>).

  • The first import is the pdfkit package
import PDFDocument from "npm:pdfkit";
  • The second import is the createWriteStream API from Node’s fs module which is provided by Deno’s standard library. The pdfkit library is very specific to Node.js. The destination should either be a file stream or HTTP response.
import { createWriteStream } from "https://deno.land/std/node/fs.ts";

Application

Once imports are in place, the application code is exactly the same as we’d have used in Node.js. The official website of pdfkit provides excellent documentation about all the APIs. We’ll see a couple of examples.

The first example is about writing a simple text in the PDF document. There are three simple steps:

  • Create a PDF document
  • Pipe the document to a file
  • Call APIs to render data into the PDF or end the PDF streaming

The following code writes a simple text in a PDF file:

import PDFDocument from "npm:pdfkit";
import { createWriteStream } from "https://deno.land/std/node/fs.ts";

const doc = new PDFDocument();
doc.pipe(createWriteStream("./out.pdf"));
doc
.fontSize(25)
.text("PDF generated from Deno", 100, 100)
.end();
console.log("PDF generated successfully");

The first example runs as follows:

> deno run -A app.ts
PDF generated successfully

The second example is a more complex one, involving 3 pages in the output PDF file. The first page contains a text and an image. The second page contains a text. The third page contains a link. The following is the code:

import PDFDocument from "npm:pdfkit";
import { createWriteStream } from "https://deno.land/std/node/fs.ts";

const linkText = "This is a link!", linkDest = "https://deno.land";

const doc = new PDFDocument();
doc.pipe(createWriteStream("./out.pdf"));
doc
.fontSize(27)
.text("This is a text block", 100, 100)
.image("./testdata/sampleLogo.png", {
fit: [300, 300],
align: "center",
valign: "center",
})
.addPage()
.fontSize(15)
.text("This is a text with smaller font", 100, 100)
.addPage()
.fontSize(25)
.fillColor("blue")
.text(linkText, 20, 20);

const width = doc.widthOfString(linkText);
const height = doc.currentLineHeight();

doc.underline(20, 20, width, height, { color: "blue" })
.link(20, 20, width, height, linkDest)
.end();
console.log("PDF generated successfully");

The second example runs as follows:

> deno run -A app.ts
PDF generated successfully

The pdfkit package works seamlessly with Deno!

More articles on similar topics can be seen in the magazine: The JS runtimes.

--

--

Articles on the popular JS runtimes, Node.js, Deno, and Bun

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store