Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mozilla pdf.js, How to I specify the filename for download?

Tags:

php

pdf.js

I pass the location of the php file that contains the following code as parameter to the viewer.html file and it is displayed correctly but when clicking the download button in the pdf viewer the document name is always document.pdf. This poses a problem because of how many mobile users will be downloading files only to discover that all of their files have the the name document.pdf and that they (for most mobile browsers) can't change the filename before downloading.

Do I have to pass some arbitrary parameter to the file or redirect to self with the filename appended?

<?php
$content = "a binary representation of my pdf";
header("Content-type: application/pdf");
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="someFile.pdf"');
echo $content;
?>
like image 271
Craig Lafferty Avatar asked Jun 27 '14 16:06

Craig Lafferty


People also ask

How do I change the name of a PDF viewer?

Once opened, click “File” then select “Info.” You will now be able to see the “Title” of the document. Select and input the title as you wish. It won't look like a text box at first but when you click on the area beside the word title, it will change into an editable text box.

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

In Chrome, the filename is derived from the URL, so as long as you are using a blob URL, the short answer is "No, you cannot set the filename of a PDF object displayed in Chrome." You have no control over the UUID assigned to the blob URL and no way to override that as the name of the page using the object element.

How do I remove download options from a PDF?

Make sure you add #toolbar=0 in the data attribute after your pdf file. It only disables the download button, It doesn't disable save as option and print option on right-click.


2 Answers

I've run into this same issue. From the pdf.js's viewer.js source:

function getPDFFileNameFromURL(url) {
  var reURI = /^(?:([^:]+:)?\/\/[^\/]+)?([^?#]*)(\?[^#]*)?(#.*)?$/;
  //            SCHEME      HOST         1.PATH  2.QUERY   3.REF
  // Pattern to get last matching NAME.pdf
  var reFilename = /[^\/?#=]+\.pdf\b(?!.*\.pdf\b)/i;
  var splitURI = reURI.exec(url);
  var suggestedFilename = reFilename.exec(splitURI[1]) ||
                           reFilename.exec(splitURI[2]) ||
                           reFilename.exec(splitURI[3]);
  if (suggestedFilename) {
    suggestedFilename = suggestedFilename[0];
    if (suggestedFilename.indexOf('%') != -1) {
      // URL-encoded %2Fpath%2Fto%2Ffile.pdf should be file.pdf
      try {
        suggestedFilename =
          reFilename.exec(decodeURIComponent(suggestedFilename))[0];
      } catch(e) { // Possible (extremely rare) errors:
        // URIError "Malformed URI", e.g. for "%AA.pdf"
        // TypeError "null has no properties", e.g. for "%2F.pdf"
      }
    }
  }
  return suggestedFilename || 'document.pdf';
}

So the majic needs to come from the URL via the reURI regexp.

What you need to do is this:

http://domain.com/path/to/Named.pdf
http://domain.com/path/to/your/api?fileId=123&saveName=Named.pdf

Each of these will result in a save as filename of Named.pdf thanks to the regexp code above.

like image 172
Campbeln Avatar answered Oct 06 '22 22:10

Campbeln


Based on comments

You can add this to wherever you're using the viewer.js file.

setTimeout(() => {
  // Wait for PDFViewerApplication object to exist
  PDFViewerApplication.setTitleUsingUrl('custom-file.pdf');
}, 10);

Then when you download the PDF it will have that filename

like image 34
Ben Winding Avatar answered Oct 06 '22 23:10

Ben Winding