简体   繁体   中英

Consume PDF stream into jsp

I am trying to display PDF report inside jsp

  1. I have PDFstream available using 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 am outputstream which is basically PDF Stream which i want to display into jsp, I dont want to set whole response contenttype as application/pdf but i want to embed pdf into part of jsp output

So i convrted pdfstream output into bytearray and trying to display using 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>

but when I am trying to execute javascript couldn't understand bytearray output it simply getting failed blob value getting displayed as below in view page source

     var blob = new Blob([%PDF-1.4
    %����
    3 0 obj
   <</Filter/FlateDecode/Length 625>>stream
   x���M��0@��>��um�'�v�������C
   ^���V�����Ķ�H����3�?�}� �    �q�D3�
   q�`�e��˼'��Do?1�(Ξ�h��2s�S(^�����gĈ^�/b��a�r"\"���_���Г�Lw

I used Iframe to solve this issue.

In iframe for src i used another jsp.

       out.write("<iframe src= displayPDF.jsp" + " " + "type=application/pdf width=100% height=600px ");
            out.write("</iframe>");

following is the displayPDF.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%><%
    byte[] pdfByteArray = (byte[]) session.getAttribute("PDFbyteArray");
    if (pdfByteArray == null)
        System.out.println("pdfByteArray is null");
    response.setHeader("Content-Disposition", "inline; filename=\"MyFile.pdf\"");
    response.setContentType("application/pdf; name=\"MyFile.pdf\"");
    ServletOutputStream serv_out = response.getOutputStream();
    serv_out.write(pdfByteArray);
    serv_out.flush();
    serv_out.close();
    out.clear();
    session.removeAttribute("PDFbyteArray");%>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM