如何在Firefox 19中显示流中的pdf?

时间:2013-02-28 08:04:04

标签: firefox pdf outputstream

我想问你如何从Firefox 19中的servlet输出pdf文件。

我有一个输出pdf流的servlet

.....

public void doPost(HttpServletRequest request, HttpServletResponse response) {




    try {

        String namePDF = "filename.pdf";
        StringBuffer sb = new StringBuffer(request.getScheme());
        sb.append("://").append(request.getServerName());
        if (request.getServerPort() > 0) {
            sb.append(":").append(request.getServerPort());
        }
        sb.append(request.getContextPath()).append("/dirPdf/")
                .append(namePDF);

        OutputStream out = response.getOutputStream();

        Document document = new Document();
        document.setMargins(30, 30, 25, 25);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter pdfWriter = PdfWriter.getInstance(document, baos);

        PdfPageEvents events = new PdfPageEvents();
        pdfWriter.setPageEvent(events);

        // step 2: we set the ContentType and create an instance of the
        // corresponding Writer
        // writer.setEncryption(null, null, PdfWriter.ALLOW_PRINTING,
        // PdfWriter.STANDARD_ENCRYPTION_40);




        document.open();

        compiledModule(request, response, document);


        document.newPage();
        pdfWriter.setPageEmpty(false);

        // chiudo il documento
        document.close();

        int tot = pdfWriter.getPageNumber() - 1;


        PdfReader reader = new PdfReader(baos.toByteArray());
        reader.consolidateNamedDestinations();
        PdfStamper pdfStamper = new PdfStamper(reader,baos);

        //read from module
        reader = new PdfReader(sb.toString());
        reader.consolidateNamedDestinations();

        PdfContentByte cbu = pdfStamper.getUnderContent(tot);
        PdfImportedPage page = pdfStamper.getImportedPage(reader, 2);

        cbu.addTemplate(page, 1, 0, 0, 1, 0, 0);

        pdfStamper.close();

        response.setContentType("application/pdf");
        response.setContentLength(baos.size());
        response.setBufferSize(baos.size());

        baos.writeTo(out);
        out.flush();
        out.close();
    } catch (Exception e) {

        logger.error("", e);
        try {
            /*if (out != null)
                out.close();*/
            response.sendRedirect("../exception.jsp?message="
                    + e.getMessage());
        } catch (IOException ex) {
            logger.error("", ex);
        }
    }
}

....

在Internet Explorer和Chrome文件中查看正确,而在Firefox 19中我收到以下错误

“此PDF文档可能无法正确显示”

我知道有一个解决方案可以通过更改Firefox 19中的设置来实现,无论如何,因为我的应用程序的许多用户可能会因为知道是否有一个实际的解决方法而感到沮丧让firefox显示pdf。

或许这是一个没有解决方法的错误?

提前谢谢!

---更新28/02/2013 12:52 PM ---

问题似乎与我使用一些ByteArrayOutputStream副本到输出流这一事实有关。 如果我直接使用outputStream一切正常。 不幸的是,我被迫使用ByteArrayOutputStream,因为我必须将pdf的最后一页粘贴到另一页。

1 个答案:

答案 0 :(得分:0)

我的案例的解决方案是更改并添加第二个ByteArrayOutputStream

.....

public void doPost(HttpServletRequest request, HttpServletResponse response) {




try {

    String namePDF = "filename.pdf";
    StringBuffer sb = new StringBuffer(request.getScheme());
    sb.append("://").append(request.getServerName());
    if (request.getServerPort() > 0) {
        sb.append(":").append(request.getServerPort());
    }
    sb.append(request.getContextPath()).append("/dirPdf/")
            .append(namePDF);

    OutputStream out = response.getOutputStream();

    Document document = new Document();
    document.setMargins(30, 30, 25, 25);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
    PdfWriter pdfWriter = PdfWriter.getInstance(document, baos);

    PdfPageEvents events = new PdfPageEvents();
    pdfWriter.setPageEvent(events);

    // step 2: we set the ContentType and create an instance of the
    // corresponding Writer
    // writer.setEncryption(null, null, PdfWriter.ALLOW_PRINTING,
    // PdfWriter.STANDARD_ENCRYPTION_40);




    document.open();

    compiledModule(request, response, document);


    document.newPage();
    pdfWriter.setPageEmpty(false);

    // chiudo il documento
    document.close();

    int tot = pdfWriter.getPageNumber() - 1;


    PdfReader reader = new PdfReader(baos.toByteArray());
    reader.consolidateNamedDestinations();
    PdfStamper pdfStamper = new PdfStamper(reader,baos2);

    //read from module
    reader = new PdfReader(sb.toString());
    reader.consolidateNamedDestinations();

    PdfContentByte cbu = pdfStamper.getUnderContent(tot);
    PdfImportedPage page = pdfStamper.getImportedPage(reader, 2);

    cbu.addTemplate(page, 1, 0, 0, 1, 0, 0);

    pdfStamper.close();

    response.setContentType("application/pdf");
    response.setContentLength(baos2.size());
    response.setBufferSize(baos2.size());

    baos2.writeTo(out);
    out.flush();
    out.close();
} catch (Exception e) {

    logger.error("", e);
    try {
        /*if (out != null)
            out.close();*/
        response.sendRedirect("../exception.jsp?message="
                + e.getMessage());
    } catch (IOException ex) {
        logger.error("", ex);
    }
}

}

...

看起来在使用PdfStamper将byteArrayOutputStream添加到自己时会出现问题。我不明白只有一个宝和两个之间的联系。随意解释......