Reputation: 308
Hi i need some help whit this : i have this java routine:
@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();
}
}
}
with that code i get a specific page of a pdf (its harcoded at this time), now i need to embed it into an existing html page. to do that i use this jquery function:
$.ajax({
type : "POST",
traditional : true,
url : "/visualizarPdf.htm",
success : function(response) {
$("#contenedor_secundario").html(response);
},
error : function(e) {
alert('Error: ' + e);
}
});
but i only get a lot of weird characters , i know i need to tell the browser i will send it a pdf file, but i dont know how.
Im using springMVC 3.0 with java, and jquery for javascript.
the workflow is: i tell the server i want a specific page, and the server responds me the page of the pdf (the page is a pdf document whit only one page) and with that i need to embed it a div. (the div's name is "contenedor_secundario").
thaks
Upvotes: 1
Views: 929
Reputation: 308
With this i call a java function, the response of that function is a pdf in binary, the objectpdf javascript library, take that binary and embed it into a the div.
$(document).ready(function() {
var pdf = new PDFObject({
url: "visualizarPdf.htm",
id: "pdfRendered",
pdfOpenParams: {
view: "FitH"
}
}).embed("pdfRenderer");
});
this is the html
<form action="visualizarPdf.htm" id ="formulario"></form>
<div id="pdfRenderer"></div>
and this is the java function thats return me the pdf in binary
@RequestMapping("/visualizarPdf.htm")
@ResponseBody
public ResponseEntity<byte[]> generatePdf() throws IOException {
PDDocument document = null;
confPdfDTO.setIndex(13);
confPdfDTO
.setDocumento("/home/ubuntu/Escritorio/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();
}
}
}
and here is the pdfObject proyect page , there you can download de lastest version. http://pdfobject.com/
yeah my english suck, sorry for that.
Upvotes: 1
Reputation: 43087
What tells the browser to either render the HTTP response body as an HTML page or try to open the result as an attachment is the Content-Disposition
header of the response.
Try setting this header in the response:
headers.set("Content-Disposition", "attachment");
With this the browser will be informed that the body of the response is an attachment, and will treat it as such, proposing to open it with the embedded PDF reader.
Upvotes: 0