Pdf file not loading properly created by the servlet

前端 未结 1 959
暗喜
暗喜 2021-01-27 13:53

Friends, I am have implemented a jsp form which takes inputs like title, description and content of the pdf file. When the jsp form is submitted, the pdf is created using itext

相关标签:
1条回答
  • 2021-01-27 14:35

    You create a PDF on your local disk and you set some headers that are sent to the browser. You do not send any bytes to the browser, hence you should not expect to see anything in your browser. This is consistent with the behavior you describe.

    In another answer, somebody tells you to write the PDF bytes to the HttpServletResponse. The most simple way to do this, is to follow the example from the book I wrote about iText, more specifically, the Hello example:

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        response.setContentType("application/pdf");
        try {
            // step 1
            Document document = new Document();
            // step 2
            PdfWriter.getInstance(document, response.getOutputStream());
            // step 3
            document.open();
            // step 4
            document.add(new Paragraph("Hello World"));
            document.add(new Paragraph(new Date().toString()));
            // step 5
            document.close();
        } catch (DocumentException de) {
            throw new IOException(de.getMessage());
        }
    }
    

    You can try this example here.

    In an ideal world, this would work for all browsers. Unfortunately, not all browsers are created alike, hence you may want to code in a more defensive way by following the PdfServlet example:

    protected void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        try {
            // Get the text that will be added to the PDF
            String text = request.getParameter("text");
            if (text == null || text.trim().length() == 0) {
                 text = "You didn't enter any text.";
            }
            // step 1
            Document document = new Document();
            // step 2
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PdfWriter.getInstance(document, baos);
            // step 3
            document.open();
            // step 4
            document.add(new Paragraph(String.format(
                "You have submitted the following text using the %s method:",
                request.getMethod())));
            document.add(new Paragraph(text));
            // step 5
            document.close();
    
            // setting some response headers
            response.setHeader("Expires", "0");
            response.setHeader("Cache-Control",
                "must-revalidate, post-check=0, pre-check=0");
            response.setHeader("Pragma", "public");
            // setting the content type
            response.setContentType("application/pdf");
            // the contentlength
            response.setContentLength(baos.size());
            // write ByteArrayOutputStream to the ServletOutputStream
            OutputStream os = response.getOutputStream();
            baos.writeTo(os);
            os.flush();
            os.close();
            // if you also want to write these bytes to a file, add:
            OutputStream fos = new FileOutputStream(pathToFile);
            baos.writeTo(fos);
            fos.flush();
            fos.close();
        }
        catch(DocumentException e) {
            throw new IOException(e.getMessage());
        }
    }
    

    You can try this servlet here. As you can see, we now create the file in memory first. We do so, so that we can set the header for the content length (some browsers require this). I've also set some other headers that may not be necessary, but that were added to my example over the years based on feedback of thousands of iText users.

    0 讨论(0)
提交回复
热议问题