Generate PDF using pdfkit in Deno

Mayank Choubey
Tech Tonic
Published in
3 min readJan 6

--

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!

--

--

Mayank Choubey
Tech Tonic

I write about Deno, Bun, and Node.js. My new book: https://choubey.gitbook.io/learn-react-in-a-day/

Recommended from Medium

Lists

See more recommendations