使用ServletOutputStream输出PDF

时间:2011-08-24 00:39:55

标签: java pdf web

我只想询问是否有使用ServletOutputStream在网页中输出PDF?例如,如果没有安装Adobe插件或未使用Chrome,我想让PDF仍然显示。

3 个答案:

答案 0 :(得分:1)

用户将 在其PC上安装某种类型的PDF查看器以打开/读取PDF文件,无论是Adobe Reader还是其他内容。您可以向用户发送PDF文件,以便在浏览器中打开或通过发送正确的HTTP标头下载(另存为附件)。具体做法是:

Content-type: application/pdf
Content-Disposition: attachment; filename=downloaded.pdf
Content-Length: XXX
Content-Transfer-Encoding: base64

Content-Disposition标题是建议下载vs“在浏览器中打开”的标题。一旦您  已发送标题,发送空白行,然后写出您的数据(通常是base64编码)。

答案 1 :(得分:0)

您的servlet可以输出它想要的任何类型的文档;但是,浏览器只会显示它知道如何的文档(例如HTML,文本,GIF,PNG,JPG等)或通过插件(PDF,SWF等)。

如果您的servlet输出有效的PDF文档并且浏览器无法在本机显示(如Chrome可以)或无法加载插件来呈现它(如Adobe),那么它可能会要求用户保存它或选择一个可以显示它的程序。

答案 2 :(得分:0)

+1因为是一个有效的问题。

客户端

您可以使用来自mozilla的pdf.js javascript库将pdf显示为图像

服务器端

如果您的pdf位于文件系统modified from here

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String pdfFileName = "pdf-test.pdf";
    String contextPath = getServletContext().getRealPath(File.separator);
    File pdfFile = new File(contextPath + pdfFileName);

    response.setContentType("application/pdf");
    response.addHeader("Content-Disposition", "inline; filename=" + pdfFileName)
    response.setContentLength((int) pdfFile.length());

    FileInputStream fileInputStream = new FileInputStream(pdfFile);
    OutputStream responseOutputStream = response.getOutputStream();
    int bytes;
    while ((bytes = fileInputStream.read()) != -1) {
        responseOutputStream.write(bytes);
    }
}

如果您使用pdfbox

等库在内存中生成pdf
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    int marginTop = 30;
    String title = "HELLO WORLD";

    final PDFont fontData = PDType1Font.HELVETICA;
    final int fontSize = 8;

    //java8 color issue
    System.setProperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider");

    final PDPage page = new PDPage();
    final PDFont fontLabel = PDType1Font.HELVETICA_BOLD;
    final int titleFontSize = 12;
    float heightLabelText = fontLabel.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;

    try (final PDDocument pdf = new PDDocument())
    {
        pdf.addPage(page);
        try (PDPageContentStream stream = new PDPageContentStream(pdf, page)) {
            float top = page.getMediaBox().getHeight() - marginTop;

            float titleWidth = fontLabel.getStringWidth(title) / 1000 * titleFontSize;

            stream.setFont(fontLabel, titleFontSize);
            stream.beginText();
            stream.newLineAtOffset((page.getMediaBox().getWidth() - titleWidth) / 2, top - 25);
            stream.showText(title);
            stream.endText();
        }
        try(ByteArrayOutputStream output = new ByteArrayOutputStream()) {
            pdf.save(output);
            resp.setContentType("application/pdf");
            resp.addHeader("Content-Type", "application/pdf");
            resp.addHeader("Content-Disposition", "inline; filename=\"yourFile" + "-" + new Random().nextInt() + ".pdf" + "\"");
            byte[] bytes = output.toByteArray();
            resp.getOutputStream().write(bytes);
            resp.setContentLength((int) bytes.length);
        }

    }
}