How to change filename for download?

6

I am generating a PDF file and use HTML5 to display it on the screen:

 $("#conteudo-pdf").append('<object data="' + meuData + '" type="application/pdf" style="width: 100%; height: 100%"></object>')

Content is displayed correctly, and a toolbar is created where you can download the file:

Firefox:

Chrome:

IE:

Opera:

By clicking "Download" the following screen opens;


Currently,foreachbrowser,adifferentdefaultfilenameisgenerated.Inthiscase,Firefoxcomespre-definedas"document.pdf". Home I would like to change it to a one my preference name that would work in all browsers. Home Is this possible?

    
asked by anonymous 23.10.2015 / 14:26

1 answer

5

If you can edit the toolbar test this:

<a href="download/document.pdf" download="nome_desejado">Download PDF</a>

Based on your review I decided to edit the answer!

After analyzing an open pdf in FireFox, I noticed that it loaded a js named "viewer.js" and look what an interesting function inside it:

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';
}

Now, you already know how to get the default name "document.pdf".

    
23.10.2015 / 14:35