How can I display a PDF report inside a JSP?
I have the PDFstream available using the following code
private void generatePDFReport(OutputStream stream, JasperPrint jasperPrint) throws JRException { JRPdfExporter jrpdfexporter = new JRPdfExporter(); jrpdfexporter.setExporterInput(new SimpleExporterInput(jasperPrint)); jrpdfexporter.setExporterOutput(new SimpleOutputStreamExporterOutput(stream)); SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration(); jrpdfexporter.setConfiguration(configuration); jrpdfexporter.exportReport();
}
Now I have an outputstream which is essentially a PDF Stream that I want to display in a JSP. I don't want to set the whole response content type as application/pdf, but I want to embed the PDF into part of the JSP output
So, I converted the pdfstream output into a byte array and am trying to display it using the following
<script language="Javascript">
function loadDoc() {
alert('called loadDoc');
var xhr = new XMLHttpRequest();
alert('called xhr');
xhr.responseType = 'arraybuffer';
alert('called responseType');
alert('called xhr.onload ');
// Create the Blob URL:
var buffer = xhr.response;
var blob = new Blob([<%=byteCharSet%>], {
type: 'application/pdf'
});
var objectURL = URL.createObjectURL(blob);
alert(objectURL);
// Create an iframe to demonstrate it:
var iframe = document.createElement('iframe');
iframe.className = 'sample-iframe';
iframe.src = objectURL;
document.body.appendChild(iframe);
console.log(objectURL);
//xhr.open('GET', 'https://cors-anywhere.herokuapp.com/http://www.xmlpdf.com/manualfiles/hello-world.pdf', true);
//xhr.send();
}
</script>
However, when I try to execute JavaScript, it couldn't understand the byte array output and simply fails. The blob value gets displayed as shown below in the view page source
var blob = new Blob([%PDF-1.4
%
3 0 obj
<</Filter/FlateDecode/Length 625>>stream
xM0@>um'vC
^VĶH3?} qD3
q`e˼'Do?1(Ξh2sS(^gĈ^/bar"\"_ГLw