php - Mozilla pdf.js, How to I specify the filename for download? -
i pass location of php file contains following code parameter viewer.html
file , displayed correctly when clicking download button in pdf viewer document name document.pdf
. poses problem because of how many mobile users downloading files discover of files have the name document.pdf
, (for mobile browsers) can't change filename before downloading.
do have pass arbitrary parameter file or redirect self filename appended?
<?php $content = "a binary representation of pdf"; header("content-type: application/pdf"); header('content-transfer-encoding: binary'); header('content-disposition: attachment; filename="somefile.pdf"'); echo $content; ?>
i've run same issue. pdf.js's viewer.js source:
function getpdffilenamefromurl(url) { var reuri = /^(?:([^:]+:)?\/\/[^\/]+)?([^?#]*)(\?[^#]*)?(#.*)?$/; // scheme host 1.path 2.query 3.ref // pattern 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 file.pdf try { suggestedfilename = refilename.exec(decodeuricomponent(suggestedfilename))[0]; } catch(e) { // possible (extremely rare) errors: // urierror "malformed uri", e.g. "%aa.pdf" // typeerror "null has no properties", e.g. "%2f.pdf" } } } return suggestedfilename || 'document.pdf'; }
so majic needs come url via reuri
regexp.
what need this:
http://domain.com/path/to/named.pdf http://domain.com/path/to/your/api?fileid=123&savename=named.pdf
each of these result in save filename of named.pdf
regexp code above.
Comments
Post a Comment