在Angularjs中为iframe打印功能 [英] Print functionality in Angularjs for iframe

查看:347
本文介绍了在Angularjs中为iframe打印功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须打印 iframe 中的特定文件。

I have to print a specific file which is in iframe.

我的view_file.ejs :

<div id="viewframe"> 
<iframe id="viewfile"  name="viewfile" src='https://docs.google.com/viewer?url=<%= urlencode.encode(img) %>&embedded=true' style="width: 100%;height:900px" frameborder="0" scrolling="no" seamless="" allowfullscreen="allowfullscreen">
</iframe>
</div>

这里img(我在urlencode.encode中使用的变量)是来自aws s3的链接桶

Here img(which is a variable i have used inside urlencode.encode) is a link from aws s3 bucket

打印按钮:

<div class="right_blk">
                    <span class="versions"><a ng-click="printdoc('viewframe')" href="javascript:void(0)" >Print</a></span>   
</div>

我的app.js:

$scope.printdoc1=function(doc)
{

   window.frames["viewfile"].focus();
   window.frames["viewfile"].print();
}

我从这里尝试了很多解决方案,但对我来说没什么用。有没有办法做到这一点?

I have tried many solution from here but nothing works for me. Is any way to do this?

更新

core.js:124 DOMException: Blocked a frame with origin "http://localhost:3000" from accessing a cross-origin frame.
    at b.$scope.printdoc (http://localhost:3000/js/docapp.js:46:32)
    at fn (eval at compile (http://localhost:3000/js/core.js:242:306), <anonymous>:4:162)
    at e (http://localhost:3000/js/core.js:287:342)
    at b.$eval (http://localhost:3000/js/core.js:150:347)
    at b.$apply (http://localhost:3000/js/core.js:151:52)
    at HTMLAnchorElement.<anonymous> (http://localhost:3000/js/core.js:287:394)
    at HTMLAnchorElement.dispatch (http://localhost:3000/js/jquery-3.2.1.min.js:3:10316)
    at HTMLAnchorElement.q.handle (http://localhost:3000/js/jquery-3.2.1.min.js:3:8343)

我收到此错误。

推荐答案

<iframe id="viewfile"  name="viewfile" src='https://docs.google.com/viewer?url=<%= urlencode.encode(img) %>&embedded=true' style="width: 100%;height:900px" frameborder="0" scrolling="no" seamless="" allowfullscreen="allowfullscreen">

不要将id作为参数传递,而是执行此操作:

Instead of passing id as a argument,do this:

 <span class="versions"><a  ng-click="printdoc()" href="javascript:void(0)" >Print</a></span>

这是你使用Javascript的方式:

This is how you do it using Javascript:

function printdoc()
{
   document.getElementById("viewfile").contentWindow.print(); //Iframe id
}

如果是Jquery:

$("#viewfile").get(0).contentWindow.print();

试试这个。希望它有所帮助..!

Try this one. Hope it helps..!

这篇关于在Angularjs中为iframe打印功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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