从浏览器的Javascript嵌入式打印PDF,HTML5,AngularJS [英] Print embedded PDF from browser with Javascript, HTML5, AngularJS

查看:1665
本文介绍了从浏览器的Javascript嵌入式打印PDF,HTML5,AngularJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加载一个Base64连接codeD上的PDF作为一个字符串到从我的服务器我的JavaScript。我的客户端应用程序使用AngularJS,HTML5。

I'm loading a Base64 encoded pdf as a String into my Javascript from my server. My client application is using AngularJS, HTML5.

我的HTML如下:

<div id="printablePdfContainer">
  <iframe id="printablePdf" width="100%" height="100%"></iframe>
</div>

我的JavaScript是这样的:

My Javascript looks like this:

var pdfName = 'data:application/pdf;base64,' + data[0].PrintImage;
var embeddedPdf = document.getElementById('printablePdf');
embeddedPdf.setAttribute('src', pdfName);
$scope.printDocument(embeddedPdf);

我的PrintDocument funtion看起来是这样的:

My printDocument funtion looks like this:

$scope.printDocument = function() {
      var test = document.getElementById('printablePdf');
      if (typeof document.getElementById('printablePdf').print === 'undefined') {

        setTimeout(function(){$scope.printDocument();}, 1000);

      } else {

        var x = document.getElementById('printablePdf');
        x.print();
      }
    };

PrintDocument关联功能已经从$ P $采取对现有的堆栈溢出问题(静音打印一个嵌入PDF ),这也是它作为一个答案打印嵌入的PDF。然而,这似乎并没有工作了。我总是得到未定义对

The printDocument function has been taken from a pre-existing question in stack overflow (Silent print a embedded PDF) which gives this as an answer to print an embedded pdf. However, this doesn't seem to work anymore. I'm always getting 'undefined' for the

typeof document.getElementById('printablePdf').print === 'undefined'

检查。好像.PRINT不存在或什么的。

check. Seems like .print doesn't exist or something.

所以,我的问题是这样的:我怎样才能打印HTML5的嵌入式PDF,使用Javascript,没有打开一个弹出窗口

So, my questions is this: How can I print an embedded PDF in HTML5, using Javascript, and without opening a popup window?

问候,
菲尔

推荐答案

另外这里回答:的从JavaScript嵌入标签打印PDF

我要发布什么,我在这里学到了很多的研究在未来任何人谁可能会发现此之后。

I'm going to post what I learned here after a lot of research for anyone in the future who might find this.

PDF的基于浏览器,浏览器版本,浏览器配置和操作系统不同的方式显示。有很多变数,所以我会在这里谈谈最常见的情况。

PDF's are displayed differently based on browser, browser version, browser configuration, and Operating System. There are a lot of variables so I'll talk about the most common situations here.


  • 在所有的浏览器我无法调用通过JavaScript任何形式的print()方法中,我只能够使用PdfActions。该OPENACTION会打电话打印。我嵌入到这些利用iText的PDF。

  • On all browsers I was unable to call any sort of print() method through Javascript, I was only able to use PdfActions. The OPENACTION would call print. I embedded these into the PDF using iText.

Chrome使用Adobe的浏览器,它并没有给进入任何形式的print()方法的但执行嵌入到PDF PdfActions。所以,你可以嵌入在PDF中的OpenAction,并有每当它从着眼于这些行动的任何应用程序打开PDF打印通话

Chrome uses Adobe's viewer, which doesn't give access to any sort of print() method but does execute PdfActions embedded in the PDF. So you can embed an 'OpenAction' in a PDF and have the PDF call print whenever it's opened from any application that looks at those actions.

火狐(超过某个版本,所有最新版本虽然)使用在Windows中的Adobe浏览器,它也承认PdfActions。然而,在OSX它失去支持,为观众的Adobe并切换到在Firefox浏览器(pdf.js)出炉。它不支持PdfActions。

Firefox (above a certain version, all recent versions though) uses the Adobe viewer in Windows, which also recognizes PdfActions. However, in OSX it loses support for the Adobe viewer and switches to the baked in Firefox viewer (pdf.js). Which does not support PdfActions.

IE:我真的没有太多测试的IE浏览器。主要是因为我放弃了对打印PDF的从Javascript后的Firefox没有在OSX工作(一个REQ。对我来说)。

IE: I didn't really test much on IE. Mostly because I gave up on printing PDF's from Javascript after Firefox didn't work on OSX (a req. for me).

正由我控制,所以我结束了在我的服务器制造服务变更和增加基于相同的标记,该PDF生成使用生成一个PNG一个获得PNG服务的服务器生成我的PDF的。浏览器处理图像比PDF文件,这我知道要去好多了,但是希望我也只是能够重新使用PDF生成服务,因为它是在别处使用我的code。

My PDF's were being generated by a server that I control so I ended up making service changes in my server and adding a get PNG service that generated a PNG based on the same markup that the PDF generation uses. Browsers handle images much better than PDFs, which I knew going in, but hoped that I would just be able to re-use the PDF generation service since it's used elsewhere in my code.

它不回答这个问题,但它是我所有的信息。我的建议的人谁在将来可能会发现这样的:沟PDF如果可能,在这种情况下,去简单。否则,请如果你知道如何通过JavaScript在FF preVIEW PDF阅读器在OSX调用print()更新了这个问题。

It doesn't answer the question, but it's all the information I have. My suggestion to anyone who might find this in the future: ditch PDF if possible in this case and go simpler. Otherwise, please update this question if you know how to call print() through Javascript in FF preview pdf viewer in OSX.

-Phil

这篇关于从浏览器的Javascript嵌入式打印PDF,HTML5,AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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