【问题标题】:Javascript Printing a page while looping through objectsJavascript在循环对象时打印页面
【发布时间】: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


    【解决方案1】:

    在我的实验中,我发现了这种解决方法……它被认为是一种杂耍,但在没有更优雅的解决方案的情况下,它可以工作。

    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 允许在我的系统上允许的不工作和吐出页面之间的最佳位置。

    【讨论】:

      猜你喜欢
      • 2016-01-11
      • 1970-01-01
      • 2011-11-30
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多