Javascript在对象间循环时打印页面
我有一个网页,我正在使用它通过笔记本电脑上的firefox“打印到文件”打印机将“页面”数据打印成PDF文件。我调用的代码如下所示:Javascript在对象间循环时打印页面,javascript,firefox,printing,Javascript,Firefox,Printing,我有一个网页,我正在使用它通过笔记本电脑上的firefox“打印到文件”打印机将“页面”数据打印成PDF文件。我调用的代码如下所示: document.body.controls.cmdPrint.click = function () // Create a function that will be called when this object is clicked upon {if (parseInt(document
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
document.body.sleep=function(delay)//创建一个新函数
{delay=delay | | 1;//默认延迟1秒
var timestamp=new Date();//获取当前时间
timestamp=新日期(timestamp.getTime()+(delay*1000));//添加延迟(秒)
while(new Date()
基本上,我需要一种方法来保持循环执行足够长的时间,以便在调用下一个window.Print()之前让“Print to File”通过。使用上面的sleep函数还可以防止window.print()运行,即使(当我试图将其用作修复程序时)我在window.print()命令之后立即调用了该函数
所以我问,这里有谁能为这个项目提供一个修复程序,这样我就不必手动循环浏览每个页面了(如果页面数超过10,这可能会让时间变得非常昂贵)
目前,当第二个页面尝试打印时,我得到一个弹出窗口,其中包含一个来自firefox的错误:“打印机错误-某些打印功能当前不可用。”跟踪此情况会导致PERR_不可用…可能是因为打印机(打印到文件)正在忙于打印上一页…因此我只需等待该问题得到解决,然后再进行下一次打印。一个错误处理程序来捕捉这个PERR_NOT_AVAILABLE,而不是让它作为一个弹出窗口反弹到用户(我)那里,这是一个不错的选择,尽管一种垃圾方式可以让页面以打印到文件系统可以处理的速度按顺序打印
如果window.print()在这种情况下确实返回了一个错误,我可以重新运行命令…在我的实验中,我发现了这个解决方法…这被认为是一个相当复杂的问题,但是在没有更优雅的解决方案的情况下,它是有效的
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
window.onafterprint = function () // Set up a handler for after a print operation
{setTimeout(function () // Run a delayed operation
{if (document.body.controls.cmdNext.click()) // Move to next page and if this is successful
{window.print(); // Continue printing
return false;} // Function complete: Still Printing
do // Do nothing...
{} while (document.body.controls.cmdPrevious.click()) // ...while we cycle back to the start
document.body.controls.style.pointerEvents = ''; // Release the controls lockout
this.blur(); // Blur the focus
window.onafterprint = function () {}; // Remove this handler
return true;}, 2000);}; // Function complete: Normal Termination
window.print(); // Begin printing the page
return true;}; // Function complete: Normal Termination
如果一个页面太复杂,无法在2秒内打印成PDF格式,则必须增加setTimeout()中的2000个数字来说明这一点。对于我的测试用例(当前的7页文档),2000似乎是不工作和在我的系统上以Firefox允许的速度吐出页面之间的最佳选择