Can I set the filename of a PDF object displayed in Chrome?

Chrome’s extension seems to rely on the resource name set in the URI, i.e the file.ext in protocol://domain/path/file.ext.

So if your original URI contains that filename, the easiest might be to simply make your <object>’s data to the URI you fetched the pdf from directly, instead of going the Blob’s way.

Now, there are cases it can’t be done, and for these, there is a convoluted way, which might not work in future versions of Chrome, and probably not in other browsers, requiring to set up a Service Worker.

As we first said, Chrome parses the URI in search of a filename, so what we have to do, is to have an URI, with this filename, pointing to our blob:// URI.

To do so, we can use the Cache API, store our File as Request in there using our URL, and then retrieve that File from the Cache in the ServiceWorker.

Or in code,

From the main page

// register our ServiceWorker

async function displayRenamedPDF(file, filename) {
  // we use an hard-coded fake path
  // to not interfere with legit requests
  const reg_path = "/name-forcer/";
  const url = reg_path + filename;
  // store our File in the Cache
  const store = await "name-forcer" );
  await store.put( url, new Response( file ) );

  const frame = document.createElement( "iframe" );
  frame.width = 400
  frame.height = 500;
  document.body.append( frame );
  // makes the request to the File we just cached
  frame.src = url;
  // not needed anymore
  frame.onload = (evt) => store.delete( url );

In the ServiceWorker sw.js

self.addEventListener('fetch', (event) => {
  event.respondWith( (async () => {
    const store = await"name-forcer");
    const req = event.request;
    const cached = await store.match( req );
    return cached || fetch( req );
  })() );

Live example (source)

Edit: This actually doesn’t work in Chrome…

While it does set correctly the filename in the dialog, they seem to be unable to retrieve the file when saving it to the disk…
They don’t seem to perform a Network request (and thus our SW isn’t catching anything), and I don’t really know where to look now.
Still this may be a good ground for future work on this.

And an other solution, I didn’t took the time to check by myself, would be to run your own pdf viewer.

Mozilla has made its js based plugin pdf.js available, so from there we should be able to set the filename (even though once again I didn’t dug there yet).

And as final note, Firefox is able to use the name property of a File Object a blobURI points to.
So even though it’s not what OP asked for, in FF all it requires is

const file = new File([blob], filename);
const url = URL.createObjectURL(file); = url;

Leave a Comment