English 中文(简体)
服务器上的 Chrome PDF 打印预览错误
原标题:Chrome PDF print preview error in servlet

当我试图打印由服务器生成的 pdf 时,我对 Google-chrome 的打印预览有问题。 错误只在默认的 pdf 插件中出现, 它与 Adobe pdf 插件一起工作 。 Servlet 中 pdf 输出的代码 :

response.setContentType("application/pdf");
        response.setHeader("Cache-Control","public");
        response.setHeader("Content-Disposition", "inline; filename="crreport.pdf""); 

        /*if (byteArrayInputStream != null){
            byteArray = new byte[1024];
            while((bytesRead = byteArrayInputStream.read(byteArray)) != -1) {
                response.getOutputStream().write(byteArray, 0, bytesRead);  
            }
        }else {
            throw new Exception("byteArrayInputStream is null!");
        }*/

        if (byteArrayInputStream != null){
            byteArray = new byte[byteArrayInputStream.available()];
            byteArrayInputStream.read(byteArray);
            response.setContentLength(byteArray.length);

            response.getOutputStream().write(byteArray);

        }else {
            throw new Exception("byteArrayInputStream is null!");
        }

        System.out.println("End");
        response.getOutputStream().flush();
        response.getOutputStream().close();

In Chrome errorLog when i try to preview generated pdf: Ignoring plugin with unexpected MIME type application/pdf (expected application/x-google-chrome-print-preview-pdf)

但预览有效, 如果我在铬页面右下角保存此pdf 默认保存按钮, 从本地机器打开它 。

我尝试了googleit, 但在很多关于铬的 pdf 插件的问题中找不到任何有用的信息。 任何建议如何解决这个问题吗?

最佳回答

最后,我找到了我案例中的主要问题。 我试图用日食插件的晶体报告生成pdf报告。 似乎CR4E产生的byteArrayInputStream 被损坏了, 以至于它不能在 Chrome 默认的 pdf 插件中发挥作用。 它可以与 iText 库一起工作。 在 CR4E byteArrayInputStream 的基底使用 iText 组合的 PDF 通常可以预览和打印 。

问题回答

使用可用文件长度是错误的; 按定义,它是可以在不屏蔽的情况下立即读取的部分( = 等待下一部分读取) 。 它通常会给出长度 。 响应的输出流不应关闭 。

    if (byteArrayInputStream == null){
        throw new Exception("byteArrayInputStream is null!");
    }
    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
    org.apache.commons.io.IOUtils.copy(byteArrayInputStream, byteArrayOS);

    byteArray = byteArrayOS.toByteArray();
    response.setContentLength(byteArray.length);
    response.getOutputStream().write(byteArray);

    System.out.println("End");
    response.getOutputStream().flush();
    //response.getOutputStream().close();

导致错误的原因: 可能可用的两个插件的不同资源使用方式不同 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签