【发布时间】:2016-01-05 05:13:46
【问题描述】:
我有一个网页,用于通过笔记本电脑上的 Firefox“打印到文件”打印机将数据“页面”打印为 PDF 文件。我调用的代码如下:
document.body.controls.cmdPrint.click = function () // Create a function that will be called when this object is clicked upon
{if (parseInt(document.body.controls.page.innerHTML) !== 0) // If we are not on the Front Cover
{return false;} // Function complete: Abnormal Termination
document.body.controls.style.pointerEvents = 'none'; // Lock down the controls so they cannot be interfered with
do // Do...
{window.print(); // Print this page
// document.body.sleep(); // Removed as this does not work as expected (see below...)
} while (document.body.controls.cmdNext.click()) // ...while we are able to advance.
document.body.controls.style.pointerEvents = ''; // Release the controls lockout
this.blur(); // Blur the focus
return true;}; // Function complete: Normal Termination
执行时,页面按预期翻转(因为 cmdNext.click() 函数在成功时返回 true,在最后一页并尝试前进时返回 false),但运行速度太快。即每个奇数页都被捕获,因为“打印机”不可用.... window.print() 在打印机准备好打印下一页之前被释放。
我尝试通过在循环中添加对辅助函数的引用来减慢执行速度(现已注释掉),但这只会锁定 CPU 并使打印机无法在另一个线程中处理......所以它不是有效的解决方案。这个函数(我写的但没有提供允许打印奇数页的预期缓冲)如下。
document.body.sleep = function (delay) // Create a new function
{delay = delay || 1; // Default to a delay of 1 second
var timestamp = new Date(); // Get current time
timestamp = new Date(timestamp.getTime() + (delay * 1000)); // Add in the delay (in seconds)
while (new Date() < timestamp) {} // While we are waiting for the delay, do nothing
return true;}; // Function complete: Normal Termination
基本上,我需要一种方法来保持循环执行足够长的时间,以便在调用下一个 window.print() 之前让“打印到文件”通过。使用上面的 sleep 函数也会使 window.print() 无法运行,即使(当我试图将其用作修复时)我已经在 window.print() 命令之后立即调用了这个函数。
所以我问,这里有没有人可以为这个项目提供修复,这样我就不必手动循环浏览每个页面(如果页面数超过 10,这可能会在时间上变得非常昂贵)?
目前,当第二页尝试打印时,弹出窗口显示来自 Firefox 的错误:“打印机错误 - 某些打印功能当前不可用。”跟踪这个导致 PERR_NOT_AVAILABLE....可能是因为打印机(打印到文件)正忙于打印前一页....所以我只需要等待它解决,然后再进行下一次打印。一个错误处理程序来捕获这个 PERR_NOT_AVAILABLE 而不是让它作为一个必须被点击的弹出窗口反弹给用户(我)会很好,尽管一种垃圾邮件的方式让页面像打印一样快地按顺序打印to 文件系统可以处理它们。
如果 window.print() 在这种情况下实际上返回了错误,我可以重新运行命令...
【问题讨论】:
标签: javascript firefox printing