我有一个带有AngularJS的Play 2.0应用程序用于前端。我想在iframe中加载pdf(由自己的应用程序提供)。按下按钮后,我想访问pdf中的内容并进行修改。目前,我正在页面上正确显示pdf。但是,在鼠标单击时,我无法访问其内容。为什么我无法访问其内容?
我已经尝试了好几个小时。这不应该是跨域问题,因为我自己的应用程序正在提供内容。
在conf / routes中:
GET /getRealPdf @main.source.PdfGetter.getRealPdf
在Controller PdfGetter.java中:
public Result getRealPdf() throws IOException {
byte[] bytes = loadFile("/home/me/test.pdf");
response().setContentType("application/pdf");
return ok(bytes);
}
在display.scala.html中:
var myApp = {};
myApp.module = angular.module("myApp",[]);
myApp.module.controller('MyController',['$scope', '$http', '$sce', MyController]);
function MyController($scope, $http) {
var controller = this;
this.name = "My Controller";
this.highlight = function(text) {
console.log("document domain: " + document.domain);
var pdfIframe = document.getElementById("myPdf");
console.log("pdfIframe: " + pdfIframe);
console.log("iframe contentDocument: " + leasePdfIframe.contentDocument);
console.log("iframe document: " + leasePdfIframe.document);
console.log("iframe name: " + leasePdfIframe.name);
console.log("iframe src: " + leasePdfIframe.src);
console.log("iframe srcdoc: " + leasePdfIframe.srcdoc);
// Below was attempt for when I used embed
// var svgDoc = leasePdfIframe.getSVGDocument();
// console.log("svgDoc: " + svgDoc);
// This throws error (cannot access document)!!!
var d = leasePdfIframe.contentWindow.document;
// console.log("d.innerHtml: " + d.innerHtml);
}
}
<div x-ng-app="myApp" x-ng-controller="MyController as viewer" class="container-fluid">
<div>
<iframe src="getRealPdf" id="myPdf" sandbox="allow-same-origin"></iframe>
<!-- <object data="getRealPdf" type="application/pdf" id="myPdf"></object> -->
<!-- <embed src="getRealPdf" type="application/pdf" id="myPdf"></embed> -->
</div>
<input type="text" name="myName" x-ng-click="viewer.highlight('someString')">
</div>
如果有任何帮助,从console.logs打印的内容如下:
文档域:localhost
pdfIframe:[object HTMLIFrameElement]
iframe contentDocument:null
iframe文件:undefined
iframe名称:
“iframe src:http://localhost:9000/getRealPdf”
iframe srcdoc: