Angularjs for iframe 中的打印功能



我必须打印一个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 存储桶的链接

打印按钮:

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

我的应用.js:

$scope.printdoc1=function(doc)
{
window.frames["viewfile"].focus();
window.frames["viewfile"].print();
}

我已经从这里尝试了许多解决方案,但没有任何效果。有什么办法可以做到这一点吗?

更新

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 作为参数传递,而是执行以下操作:

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

这是你如何使用Javascript来做到这一点的:

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

在 Jquery 的情况下:

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

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

最新更新