How can I modify PDF.js to accept local data?
February 2, 2021 10:06 AM   Subscribe

PDF.JS is set up to accept only data, http, and https so it can stream that data rather than loading all of it. I want to load all of it. The extension PDF.js in chrome allows one to read local data. How can I change the PDF.js code so that it will accept local PDF files?
posted by metatuesday to Computers & Internet (3 answers total) 1 user marked this as a favorite
 
This doc explains how to read a File, which you can obtain from an input with type=file to a data-URL
posted by gregjones at 11:14 AM on February 2, 2021


You could set up a form that accepts files via drag-and-drop using the FileReader interface, like gregjones says. The result is a file blob object, which can be converted to an array buffer that the PDF.js file input API accepts. Note arrayBuffer() is an async method so you have to know how to use Promises.

Getting it to load local files directly is nontrivial. Browsers severely lock down local file access from JS, for good reasons. I imagine Chrome cheats by giving itself an exemption to this.
posted by neckro23 at 12:22 PM on February 2, 2021


I imagine Chrome cheats by giving itself an exemption to this.

PDF.js for Chrome is a browser extension (not part of Chrome itself), which means it can access local files by requesting access to the file:// URL schema in its manifest. So it's not really "cheating", but it's definitely not an option that's available to ordinary websites, either.
posted by teraflop at 4:21 PM on February 2, 2021


« Older Correct way to write a number range?   |   What are your favorite (savory-ish) decaf teas and... Newer »
This thread is closed to new comments.