【问题标题】:Specify file name when printing page element using javascript使用 javascript 打印页面元素时指定文件名
【发布时间】:2019-08-15 14:53:24
【问题描述】:

使用 javascript 打印页面元素时,我需要指定文件名。

这是我的代码,它将从页面打印指定的elementId

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

这工作正常,除了出现在打印对话框中的默认文件名是download.pdf,我需要根据来自 url 的参数指定我自己的文件名:

https://example.com/a/b/c/123456789/2019/1

我想使用 URL 的最后三个元素构造一个文件名,这样打印对话窗口中显示的默认文件名是:

123456789_2019_1.pdf

【问题讨论】:

    标签: javascript pdf document.write


    【解决方案1】:

    看看这个:How to set a file name using window.open

    似乎是一种变通方法,但很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-08
      • 2011-09-22
      • 1970-01-01
      • 2011-11-30
      • 1970-01-01
      • 2012-02-08
      相关资源
      最近更新 更多