【发布时间】:2014-05-30 09:04:06
【问题描述】:
嗨,我需要一些帮助:我有这个 java 例程:
@RequestMapping("/visualizarPdf.htm")
@ResponseBody
public ResponseEntity<byte[]> generatePdf() throws IOException {
PDDocument document = null;
confPdfDTO.setIndex(13);
confPdfDTO.setDocumento("/Users/martinLequerica/Desktop/directoriosServidor/republica 4.pdf");
try {
document = new PDDocument();
// PDPage page = new PDPage();
PDPage page = corta.cut(confPdfDTO);
document.addPage(page);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
document.save(baos);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(new MediaType("application", "pdf"));
headers.setContentLength(baos.toByteArray().length);
return new ResponseEntity<byte[]>(baos.toByteArray(), headers,
HttpStatus.CREATED);
} catch (Exception e) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);
return new ResponseEntity<byte[]>("BROKEN".getBytes(), headers,
HttpStatus.CREATED);
} finally {
if (document != null) {
document.close();
}
}
}
使用该代码,我获得了 pdf 的特定页面(此时它已被编码),现在我需要将其嵌入到现有的 html 页面中。为此,我使用了这个 jquery 函数:
$.ajax({
type : "POST",
traditional : true,
url : "/visualizarPdf.htm",
success : function(response) {
$("#contenedor_secundario").html(response);
},
error : function(e) {
alert('Error: ' + e);
}
});
但我只得到很多奇怪的字符,我知道我需要告诉浏览器我会给它发送一个 pdf 文件,但我不知道如何。
我在 java 中使用 springMVC 3.0,在 javascript 中使用 jquery。
工作流程是:我告诉服务器我想要一个特定的页面,服务器响应我的 pdf 页面(该页面是一个只有一页的 pdf 文档)并且我需要将它嵌入一个 div。 (div 的名称是“contenedor_secundario”)。
谢谢
【问题讨论】:
标签: java jquery spring-mvc pdf embed