Print functionality in Angularjs for iframe

自闭症网瘾萝莉.ら 提交于 2020-02-05 04:25:08

问题


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

My 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>

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

Print button:

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

my 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?

Update

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)

I am getting this error.


回答1:


<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">

Instead of passing id as a argument,do this:

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

This is how you do it using Javascript:

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

In case of Jquery:

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

Try this one. Hope it helps..!



来源:https://stackoverflow.com/questions/48786719/print-functionality-in-angularjs-for-iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!