无法从Servlet打开PDF

时间:2015-01-26 13:36:13

标签: servlets itext

我试图在创建后立即打开PDF。 我希望在浏览器中看到结果,但我选择了。 顺便说一下,程序在服务器上运行。 如果我尝试在那里打开PDF,没问题 - 一切正常。 如果客户点击了按钮,则客户无法看到pdf。

这是我的代码:

@WebServlet("/GeneratePdfCustomer")
public class GeneratePdfCustomer extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    final int id = Integer.parseInt(request.getParameter("id"));
    makePdf(request, response, "POST",id);

}

 public void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
    doPost(request, response);
      }

 public void makePdf(HttpServletRequest request,
          HttpServletResponse response, String methodGetPost, int id)
          throws ServletException, IOException {

        response.setContentType("application/pdf");


        try {

        Document document = new Document();         

        PdfWriter.getInstance(document, response.getOutputStream());

        document.open();

        document.add(new Paragraph("Test"));

        document.close();

        } catch (Exception e) {
          System.out.println("Error" + e);
        }
      }
}

我使用 iText 库。

修改

现在我已经更改了我的servlet代码,但仍然无法为我工作。 我无法在浏览器中看到pdf。 Firefox和IE都没有显示任何内容。

package de.WorldCheckCustomer.model;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

@WebServlet("/GeneratePdfCustomer")
public class GeneratePdfCustomer extends HttpServlet {

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    makePdf(request, response);
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    makePdf(request, response);
}

public void makePdf(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {


    // Set content type to application / pdf
    // browser will open the document only if this is set
    response.setContentType("application/pdf");
    // Get the output stream for writing PDF object
    OutputStream out = response.getOutputStream();
    try {
        Document document = new Document();
        /* Basic PDF Creation inside servlet */
        PdfWriter.getInstance(document, out);
        document.open();
        document.add(new Paragraph("Tutorial to Generate PDF using    Servlet"));
        document.add(new Paragraph(
                "PDF Created Using Servlet, iText Example Works"));
        document.close();
    } catch (DocumentException exc) {
        throw new IOException(exc.getMessage());
    } finally {
        out.close();
    }
}

private static final long serialVersionUID = 6067021675155015602L;

}

0 个答案:

没有答案