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;
?>
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With