Urgent.News

What's breaking now, across thousands of outlets.

Tech

A PDF Exporter With No PDF Library

Longshot, a Firefox screenshot extension I have been building, exports to PDF. The exporter is 336 lines and pulls in nothing. That is not a boast about writing things from scratch, which is usually a bad reason to do anything; it is that the two jobs a PDF library would do here are both already done by the browser, and once you notice that, the remaining work is smaller than the dependency would…

Longshot, a Firefox screenshot extension designed by me, exports screenshots as PDF files. Strikingly, the exporter is only 336 lines of code and relies on no external libraries. This is not due to a lack of relying on libraries to write everything, but rather because the PDF library would have performed two functions: handling image data and compressing data.

In this case, these functions are already taken care of by the browser itself. The /DCTDecode filter indicates that the PDF stores image data as a JPEG bitstream, and the /FlateDecode filter indicates that the stream is zlib-wrapped deflate. Both of these filters can be handled natively by the browser through canvas.toBlob and CompressionStream.

Consequently, the code for this exporter is considerably shorter than that of a general PDF library, which would need to handle typography, fonts, vector graphics, encryption, forms, and incremental updates among other features.

One potential pitfall with PDF files is the cross-reference table, which provides a byte offset for each object in the file. A single incorrect offset can render the entire document unreadable. Unlike higher-level components, this issue cannot be caught by the exporting process and will not be noticed until the file is opened later. To avoid this, the test suite checks the existence and correctness of the cross-reference table by seeking to the specified offsets and verifying the objects they point to.

The design of Longshot's exporter is such that it does not use any canvas APIs, thanks to a separate bridge component. This design decision allows the byte-level testing to occur in Node, without the need for a browser environment. The exporter also includes features such as splitting the document at page height for readability, using an invisible OCR layer with selectable and searchable text, and discarding low-confidence OCR recognitions. These choices help ensure that the searchable layer is both accurate and useful for users.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

React Context Is Not State Management: Stop Using It

The Architectural Trap: Why React Context Isn't a State Manager In the modern React ecosystem, "prop drilling" is often cited as the ultimate developer productivity killer.

  • React Context is a dependency injection mechanism, not a state management tool.
  • Misusing Context with objects causes unnecessary re-renders throughout the component tree.
  • Split providers and use external stores for frequently changing data to improve performance.

More from Wednesday 9 September →