我有一个现有的servlet,可以提供给我一个pdf字节数组:
byte[] pdf = myService.getPdf(myArgs);
response.setContentType("application/pdf");
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, postcheck=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setContentLength(pdf.length);
ServletOutputStream out = response.getOutputStream();
out.write(pdf);
out.flush();
out.close();
// for test: byte[] pdf
File testfile = new File("C:\\mytestpath\\mytest.pdf");
FileOutputStream fos = new FileOutputStream(testfile);
fos.write(pdf);
fos.flush();
fos.close();
我在servlet中写了一个测试pdf文件来检查myService.getPdf()的返回值。测试文件没问题,我可以在Acrobat Reader中打开这个有效的文件。
现在是我的JQuery-JavaScript代码:
function generatePDF(url) {
currentRequest = $.ajax({
url: url,
type: "GET",
success: function(data) {
var blob = new Blob([data], {type: 'application/pdf'});
var fileURL = window.URL.createObjectURL(blob);
window.open(fileURL, "_blank");
},
error: function() {
console.error("test in generatePDF error");
// use here other jquery mobile functions
}
});
}
我想在我的servlet中生成一个PDF字节数组,并在新的浏览器选项卡中显示PDF。通过我的实现,会打开一个新的浏览器选项卡(地址行:blob:http%3A // localhost%3A8080 / 3fd5808b-758b-4076-94c9-af9884f631a3)。但是PDF文件为空,PDF文件的页面大小正确。我该如何解决这个问题?我需要在新的浏览器选项卡中获得名为myid.pdf的有效PDF。谢谢您的提示,Thomas。