打印pdf而不用目视打开它 [英] Print a pdf without visually opening it

查看:105
本文介绍了打印pdf而不用目视打开它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要构建的东西是通过单击一个按钮来触发PDF文件的打印,但是没有打开它.

The thing I want to build is that by clicking a button I want to trigger the print of a PDF file, but without opening it.

+-----------+
| Print PDF |
+-----------+
     ^ Click *---------> printPdf(pdfUrl)

我第一次尝试的方法是使用iframe:

The way how I first tried it is to use an iframe:

var $iframe = null;

// This is supposed to fix the onload bug on IE, but it's not fired
window.printIframeOnLoad = function() {
  if (!$iframe.attr("src")) { return; }
  var PDF = $iframe.get(0);
  PDF.focus();

  try {
    // This doesn't work on IE anyways
    PDF.contentWindow.print();

    // I think on IE we can do something like this:
    // PDF.document.execCommand("print", false, null);
  } catch (e) {
    // If we can't print it, we just open it in the current window
    window.location = url;
  }
};

function printPdf(url) {

  if ($iframe) {
    $iframe.remove();
  }

  $iframe = $('<iframe>', {
    class: "hide",
    id: "idPdf",
    // Supposed to be a fix for IE
    onload: "window.printIframeOnLoad()",
    src: url
  });

  $("body").prepend($iframe);
}

这适用于Safari(台式机和iOS)和Chrome(我们可以将其推广到Webkit吗?).

This works on Safari (desktop & iOS) and Chrome (can we generalize it maybe to webkit?).

在Firefox上,PDF.contentWindow.print()permission denied错误结尾(即使pdf是从同一域加载的).

On Firefox, PDF.contentWindow.print() ends with a permission denied error (even the pdf is loaded from the same domain).

在IE(11)上,onload处理程序不起作用.

On IE (11), the onload handler is just not working.

现在,我的问题是:还有另一种更好的方式来打印pdf,而无需向用户可视地打开它?

Now, my question is: is there another better way to print the pdf without visually opening it to the user?

跨浏览器在这里至关重要.我们应该支持尽可能多的浏览器.

The cross browser thing is critical here. We should support as many browsers as possible.

实现此目标的最佳方法是什么?我的开始好吗?如何完成?

What's the best way to achieve this? Is my start a good one? How to complete it?

我们现在是2016年,我觉得在所有浏览器中实现它仍然很痛苦.

推荐答案

更新:此

UPDATE: This link details an elegant solution that involves editing the page properties for the first page and adding an action on Page Open. Works across all browsers (as browsers will execute the JavaScript placed in the actions section). Requires Adobe Acrobat Pro.

看来2016年在打印问题上没有新的进步.有一个类似的问题,我使用 PDF.JS 解决了跨浏览器的问题,但是必须对源代码进行单线添加(无论如何,他们都要求您以它为基础).

It seems 2016 brings no new advancements to the printing problem. Had a similar issue and to make the printing cross-browser I solved it using PDF.JS but had to make a one-liner addition to the source (they ask you to build upon it in anyways).

想法:

  • Download the pre-built stable release from https://mozilla.github.io/pdf.js/getting_started/#download and add the "build" and "web" folders to the project.
  • The viewer.html file is what renders out PDFs with a rich interface and contains print functionality. I added a link in that file to my own JavaScript that simply triggers window.print() after a delay.

添加到查看器的链接:

    <script src="viewer.js"></script>
    <!-- this autoPrint.js was added below viewer.js -->
    <script src="autoPrint.js"></script>
</head>

autoPrint.js javascript:

The autoPrint.js javascript:

(function () {
    function printWhenReady() {
        if (PDFViewerApplication.initialized) {
            window.print();
        }
        else {
            window.setTimeout(printWhenReady, 3000);
        }
    };

    printWhenReady();
})();

  • 然后我可以在iframe的src中发出对viewer.html?file=的调用并将其隐藏.由于使用Firefox,不得不使用可见性,而不是显示样式:

    • I could then put calls to viewer.html?file= in the src of an iframe and hide it. Had to use visibility, not display styles because of Firefox:

      <iframe src="web/viewer.html?file=abcde.pdf" style="visibility: hidden">
      

    • 结果:在短暂的延迟后显示了打印对话框,对用户隐藏了PDF.

      The result: the print dialog showed after a short delay with the PDF being hidden from the user.

      在Chrome,IE,Firefox中进行了测试.

      Tested in Chrome, IE, Firefox.

      这篇关于打印pdf而不用目视打开它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆