I need to specify a filename when printing a page element using javascript.
Here is my code that will print the specified elementId
from the page:
function printPartOfPage(elementId) {
var printHeader = document.getElementById('header');
var printContent = document.getElementById(elementId);
var windowUrl = 'NewWindow';
var uniqueName = new Date();
var windowName = 'Print' + uniqueName.getTime();
var printWindow = window.open(windowUrl, windowName, 'left=20,top=200');
printWindow.document.write('<html xmlns="http://www.w3.org/1999/xhtml"><head>');
printWindow.document.write('<link href='+sURL+'css/style.css rel="stylesheet">');
printWindow.document.write('<link href='+sURL+'css/print.css rel="stylesheet">');
printWindow.document.write('</head><body>');
printWindow.document.write(printContent.innerHTML);
printWindow.document.write('</body></html>');
printWindow.document.close();
printWindow.focus();
setTimeout(function(){
printWindow.print();
}, 500)
printWindow.setTimeout("window.close()", 2000);
}
This works fine, except that the default file name that appears in the print dialog is download.pdf
and I need to specify my own file name based on parameters from the url:
https://example.com/a/b/c/123456789/2019/1
I would like to construct a file name using the last three elements of the URL such that the default file name that appears in the print dialog window is:
123456789_2019_1.pdf