DEV Community

Daniel Pertu
Daniel Pertu

Posted on

Adding a QR code to our poster makes the PDF 246 times bigger, and 99.5 per cent of the file is one uncompressed bitmap

PubTrivia's poster maker builds an A4 quiz night poster in the browser and hands you a PDF. Leave the QR field empty and the file is 4,409 bytes. Type a web address into it and the same poster is 1,085,526 bytes.

That is 246 times larger for one black and white square 38 millimetres across. Here is where it goes, and what it does to the code on the paper.

The file

Both downloads were taken off the live page, then rebuilt in Node from the same two libraries at the same versions (qrcode 1.5.4, jspdf 4.2.1) with the same inputs. After normalising /CreationDate and the file /ID, the rebuilds are byte for byte identical to what the browser produced: 4,319 and 1,085,436 bytes of comparable content. So everything below is the artefact a visitor actually gets, not a reconstruction of it.

Inside the larger file there is exactly one image object:

/Type /XObject
/Subtype /Image
/Width 600
/Height 600
/ColorSpace /DeviceRGB
/BitsPerComponent 8
/Length 1080000
Enter fullscreen mode Exit fullscreen mode

600 x 600 x 3 bytes = 1,080,000, which is 99.49 per cent of the whole document. There is no /Filter key, so nothing is compressed: a two-colour image is stored as three bytes per pixel of raw RGB, and every one of those bytes is either 0 or 255.

The same thing happens on the QR code generator next door, whose "A4 sheet of table cards" button produces a 790,478-byte PDF. One 512 x 512 image at /Length 786432, four Do operators drawing it into four cells. jsPDF is at least smart enough to store the bitmap once and reference it four times; it just stores it uncompressed.

artefact                                 bytes        image stream   share
---------------------------------------  -----------  -------------  ------
poster, no QR code                             4,409              0      0%
poster with a QR code                      1,085,526      1,080,000  99.49%
A4 sheet of four table cards                 790,478        786,432  99.49%
single QR code as a PNG download               8,877          (file)   100%
Enter fullscreen mode Exit fullscreen mode

One flag

jsPDF's constructor takes compress. It defaults to false, and the two tools call it like this:

new jsPDF({ orientation: "portrait", unit: "mm", format: "a4" })
Enter fullscreen mode Exit fullscreen mode

Rebuilding the same poster with compress: true and nothing else changed:

poster with a QR code, compress: false    1,085,526 bytes
poster with a QR code, compress: true         6,909 bytes
poster, no QR code,    compress: true         3,659 bytes
Enter fullscreen mode Exit fullscreen mode

157 times smaller. The QR code, which was 1.08 MB of the file, becomes about 3,250 bytes once a flate filter is allowed near it, because a QR code is the most compressible thing you will ever put in a PDF. A post about the same project's answer sheets two days ago measured the same missing flag costing 82 per cent on a document with no images in it at all. With an image in it, the same flag is worth 99.4 per cent.

An aside that cost me an hour. The PNG the browser hands to jsPDF is 8,877 bytes for the 512-pixel code. The same code, same options, encoded in Node, is 3,146 bytes. Both are valid PNGs of identical pixels; one came out of a canvas and the other out of pngjs. If you are measuring a browser-generated file, generate it in the browser.

What 38 millimetres does to the code

The interesting part is not the bytes, it is the geometry, because a QR code has a physical minimum and a poster has a physical size.

For a typical join link of 29 modules:

                        placed at   pixels   effective   mm per     quiet zone
                                             resolution  module     in the PNG
----------------------  ----------  -------  ----------  ---------  ----------
poster QR slot              38 mm   600 px     401 dpi    1.226 mm   1.03 modules
table card on A4            78 mm   512 px     167 dpi    2.364 mm   2.06 modules
Enter fullscreen mode Exit fullscreen mode

Both have far more pixels than print needs. Both have less quiet zone than the specification asks for: ISO/IEC 18004 wants four modules of clear space on every side, the table cards ship two, and the poster ships one.

The poster gets away with it by accident. The layout draws a white 44 mm panel behind the 38 mm code, so there are 3 mm of white either side of the PNG's own 1.27 mm, which comes to about 3.5 modules of quiet zone against a navy background. Take the panel away and a code printed on the dark theme would have one module of white around it. The table cards get away with it because they are printed on white paper, and paper is quiet zone.

Then there is the thing I did not expect. The width option in qrcode scales to the pixel count you asked for, not to a whole multiple of the module size. 512 pixels over 33 module-widths is 15.515 pixels each, so the modules cannot all be the same size. Decoding the actual PNG that the live tool produces and measuring run lengths along a timing row:

.32 #108 .16 #15 .16 #15 .16 #15 .16 #15 .16 #15 .16 #15 .16 #15 .16 #108 .31
Enter fullscreen mode Exit fullscreen mode

Dark modules 15 pixels, light modules 16, finder patterns 108, and the quiet zone 32 pixels on the left against 31 on the right. Scanners cope with that easily; it is just not the clean integer grid you would draw by hand, and it is the reason to ask for a multiple of the module count rather than a round number of pixels.

The length limit that is really a module limit

The poster's URL field caps input at 300 characters. That cap is not about layout, it is about how small the modules get, and nobody appears to have joined the two numbers up:

address length   version   modules   mm per module at 38 mm
--------------   -------   -------   ----------------------
25 chars            v2        25            1.407 mm
36 chars            v3        29            1.226 mm
51 chars            v4        33            1.086 mm
130 chars           v7        45            0.809 mm
300 chars          v13        69            0.535 mm
Enter fullscreen mode Exit fullscreen mode

A 300-character tracking URL on an A4 poster gives you half-millimetre modules. That is readable by a phone held still at close range and nothing like the two seconds of attention a poster in a chip shop window gets. The common rule of thumb is that a code is read from about ten times its own width, so even the healthy version of this is a 38 cm code, which means the poster has to be reachable rather than across the room.

The QR generator next door has no length limit at all. A 2,000-character address gives you a version 40 code, 177 modules, a 33,839-byte PNG, and at 78 mm on a table card, 0.42 mm per module.

One stale comment

While reading the code for this I found that the table card sheet's own doc comment says sixteen cards:

one code is a download, sixteen identical table cards is a print job
Enter fullscreen mode Exit fullscreen mode

The loop is cols = 2, rows = 2. It makes four, the page copy says four, and the comment is the only thing in the repository that still says sixteen. Harmless, but it is the kind of drift worth grepping for when a number appears in prose next to the code that produces it.

What I would change

  1. compress: true in both constructors. Two words, 99.4 per cent of the poster file, and it also fixes the answer sheets.
  2. Ask qrcode for a pixel size that is a whole multiple of the module count, or just hand jsPDF a bigger margin and let the printer do the scaling.
  3. Raise the quiet zone to the specified four modules in the generator, where the code lands on white paper anyway and costs nothing.
  4. Tie the URL field's limit to the module count rather than a round 300, and warn when the resulting code goes below about 0.8 mm per module.
  5. Delete "sixteen" from the comment.

Everything here is reproducible without an account: open the QR generator, type any address, press "A4 sheet of four", and look at the size of the file you just saved. 790 KB, for four copies of a black and white square.

Top comments (0)