Javascript循环对象时打印页面
Javascript Printing a page while looping through objects
我有一个网页,通过笔记本电脑上的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
基本上,我需要的是一种保持循环执行足够长时间的方法,以便在调用下一个窗口.Print()之前让"打印到文件"通过。使用上面的sleep函数也可以阻止window.print()的运行,即使(当我试图将其用作修复程序时)我已经在window_print()命令之后立即调用了该函数。
所以我想问,这里有人能为这个项目提供一个修复程序吗?这样我就不必手动循环浏览每个页面(如果页面数超过10,这可能会让时间变得非常昂贵)?
目前,当第二页尝试打印时,我会看到弹出窗口,其中包含一个来自firefox的错误:"打印机错误-某些打印功能当前不可用。"跟踪此错误会导致PERR_not_available。。。。可能是因为打印机(打印到文件)正忙于打印前一页。。。。所以我只需要等待问题解决,然后再进行下一次打印。一个错误处理程序来捕捉这个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允许的最快速度吐出页面之间的最佳点。
- $.每个jquery循环打印一个“;未定义的“;对于getJSON请求后的每个元素,网格数据都会完美地打印出来
- 如何在javascript循环中打印代码
- 无法使用JavaScript打印带有for循环的验证错误
- 使用 for 循环 JavaScript 的打印范围
- 使用循环打印时间列表
- javascript使用循环的打印时间
- 循环访问日期并打印格式化日期
- 循环使用JSONObject并打印html
- PHP:从数据列表中打印(循环困难)
- 我如何做一个纯Javascript foreach循环并打印出结果firebase
- 从数组中快速打印对象,然后循环
- For循环只打印一个DIV标记.不是8个DIV标签
- 如何使用嵌套循环使用 Javascript 打印到外部循环的输出中
- for 循环无法打印不正确的值.需要索引
- 如何在 Java 脚本中打印大括号循环
- 对于我需要打印出从 1 到 20 的数字的循环
- 打印每个循环,而不仅仅是最终答案
- 如何使用护照循环用户.js并将用户列表打印到 DOM 上
- 对于在循环中不打印所有元素 - Javascript
- 如何在javascript中实现漂亮的打印循环双链表