我无法使用此表单/ js组合动态显示PDF。我让用户进入他们的文件系统,找到PDF,并在屏幕上显示文档。我可能需要使用AJAX并最终将pdf从jsp / html发送到servlet并获取响应以显示在屏幕上,但我不想这样做。抛弃循环的是,在PDFObject参数中,当我输入pdf“/Indexer/pdf/TEST.pdf”的直接URL时,它就像一个魅力。但是,当用户进入他们的文件系统并选择他们自己的文档进行查看时,它根本不做任何事情。这是代码:
<div id="uploadPDFForm">
<h3>Get PDF</h3>
<form id="pdfUpload" action="IndexServlet" method="get" enctype="multipart/form-data">
<input type="file" name="document" value="" id="pdfLocation"><br>
<input type="submit" value="View Document" id="pdfBtn">
<input type="button" value="pressMe" id="pdfBtn2">
</form>
</div>
<p id="statement">Statement that is replaced with PDFembed message using <em>button click</em> </p>
<div id="pdf">
</div>
<script type="text/javascript" src="/Indexer/_js/jquery-1.10.2.js"></script>
<script type="text/javascript" src="/Indexer/_js/pdfobject.js"></script>
<script type="text/javascript">
var str = $("#pdfLocation").val();
var res = str.replace(/\\/g, "/");
var pObj = new PDFObject({
url: res,
//url: $("#pdfLocation"),
//url: "/Indexer/pdf/TEST.pdf",
id: "myPDF",
pdfOpenParams: {
navpanes: 0,
toolbar: 0,
statusbar: 0,
view: "FitV"
}
});
function embed(){
var s = document.getElementById("statement");
var msg;
document.getElementById("pdf").className = "embedded";
var htmlObj = pObj.embed("pdf");
if(htmlObj){
msg = "The PDF was successfully embedded";
s.className = "success";
} else {
msg = "Embed has failed.";
s.className = "fail";
}
s.innerHTML = msg;
}
$("#pdfBtn2").click(function (){
var s = document.getElementById("statement");
var msg;
if(pObj){
var plugintype = pObj.get("pluginTypeFound");
msg = "Type of PDF plugin found: " +plugintype;
if(plugintype){
msg += "<br/><a href='#' onclick='embed(); return false;'>Embed PDF</a>";
} else {
msg += "<br/>Looks like we won't be able to embed your PDF! Doh!";
s.className = "fail";
}
}
s.innerHTML = msg;
});
</script>