Case study: the PDFKit lint fix that took the colour out of every JPEG

· Nathan Broadbent

PDFKit is the PDF generator for Node.js that a lot of other tools are built on. pdfkit had about 19.1 million npm downloads in the week to 2 December 2026. It has screenshot tests: 44 snapshots of rendered pages, drawn with pdf.js and compared with jest-image-snapshot on every push, in three Node versions.

The bug

On 7 June 2026, 38dd435 ("Fix lint issues") changed one expression in the JPEG parser, from this.data[pos++] to this.data[pos + 1]. The value of pos was never read again, so to the linter the increment looked like dead code. But pos + 1 reads the next byte instead of the current one. The byte it should read is the image's channel count, 3 for a colour JPEG. The one after it is the first channel's ID, which is usually 1, and 1 channel means greyscale. So PDFKit now wrote every colour JPEG into the PDF with a greyscale colour space, /DeviceGray, instead of /DeviceRGB.

It went out in v0.19.0 the same day. It was reported quickly, and b5b2547 ("Fix RGB JPEG embedded as DeviceGray (0.19.0 regression)") shipped in v0.19.1 on 10 June. So this one lasted three days. It's in this series because of how it got past CI, not because of how long it lasted.

The replay

We forked PDFKit (visualcihq/pdfkit) and added three small documents:

  • a colour photo-style JPEG (four coloured bands and a circle) under a blue heading;
  • PDFKit's own test JPEGs on one page: four of the photos its tests already use;
  • a control with text, filled shapes and a PNG, which the JPEG parser never touches.

A GitHub Actions job builds PDFKit from the commit, renders the three PDFs and sends them to VisualCI. We pushed 38dd435's parent as a baseline and opened a pull request with 38dd435 on top.

VisualCI's build for that pull request, build #4, changed two of the three documents: both with JPEGs. On the page of PDFKit's own test images, every photo has gone grey and looks zoomed in and striped, because three channels of data are being drawn as one. The building, the bee, the waterfall and the living room are each unrecognisable. The control didn't change. We rejected the build.

PDFKit's own test JPEGs before 38dd435

The same page with 38dd435: every photo grey, zoomed in and striped

The changes VisualCI marked on build #4

Why PDFKit's own screenshots didn't catch it

The same pull request ran PDFKit's own CI, on Node 20, 22 and 24. All 367 tests passed, including the screenshot tests, and those embed colour JPEGs: eight photos in the EXIF-orientation tests and another in the pdfmake-style image tests. (Its lint was red at this commit for an unrelated reason, which upstream fixed in the next commit, so in the fork we let the tests run past it.)

It wasn't a lenient threshold. The fork also renders our three PDFs with PDFKit's own test renderer, the pdf.js helper its screenshot tests use, and counts the coloured pixels on each page. The numbers are identical before and after 38dd435: 24.68% on the colour JPEG page, 10.35% on the test images. pdf.js draws the JPEG with the colours the image file itself contains, and ignores the colour space the PDF gives it. Other PDF readers take the PDF at its word. We rendered the same PDFs with pdfium, the engine behind Chrome's PDF viewer, and with MuPDF: both draw every photo grey and striped, as VisualCI did.

So the tests were looking at the right pages, through a renderer that forgave the mistake. The fix added a unit test that checks the parsed colour space directly.

What it takes

Three PDFs, a 36-line script that makes them, and 30 seconds on GitHub's free runners. The lesson is about the renderer more than the test: a visual check is only as strict as what draws the page. Your users open your PDFs in more than one reader, and some, like Chrome's, follow the PDF to the letter. Compare them with a renderer that's as strict as the strictest reader, not as forgiving as the most forgiving.

Try it

PDFs · GitHub Actions