weixin_33694172 2019-12-12 13:24 采纳率: 0%
浏览 232

将PDF流消费到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
  • 写回答

1条回答 默认 最新

  • larry*wei 2019-12-16 12:49
    关注

    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");%>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示