-3

I use iText/Pdfbox to create a PDF document. Everything works when I create the PDF using a standalone Java class like this:

public static void main(String[] args){
...
...
...
}

The document is created correctly.

But I need create a PDF document from a Servlet. I paste the code into the get or post method, run that servlet on the server, but the PDF document isn't created!

This code works as a standalone application:

enter image description here

This code doesn't work:

enter image description here

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
alexis_dia
  • 156
  • 3
  • 13
  • 2
    Add the code as text, not as screenshot – Jens Apr 20 '16 at 13:27
  • What means `doesn't work`? Any exception? – Jens Apr 20 '16 at 13:28
  • 2
    Have you tried turning it off and on again? – Amedee Van Gasse Apr 20 '16 at 13:30
  • 2
    First choose the technology: either this is a PdfBox question or it's an iText question. You are using PdfBox code, so your question shouldn't be tagged as an iText question. Note that iText code is usually easier to read than PdfBox code. Nevertheless, I did the effort trying to read your PdfBox code. I see that you create a file on the file system. Why are you doing that? You don't send anything to the `response` object. This means that your code *shouldn't work* and you *shouldn't be surprised* that it doesn't work. – Bruno Lowagie Apr 20 '16 at 13:34
  • I mean that the code is not saved on disk.I want save pdf on my hard drive. – alexis_dia Apr 20 '16 at 14:21
  • I want save pdf on my hard drive automaticly by servlets. – alexis_dia Apr 20 '16 at 14:28

1 Answers1

0

Please read the documentation. For instance the answer to the question How can I serve a PDF to a browser without storing a file on the server side?

You are currently creating a file on your file system. You aren't using the response object, meaning you aren't sending any bytes to the browser. This explains why nothing happens in the browser.

This is a simple example:

public class Hello extends HttpServlet {
    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());
        }
    }
}

However, some browsers experience problems when you send bytes directly like this. It's safer to create the file in memory using a ByteArrayOutputStream and to tell the browser how many bytes it can expect in the content header:

public class PdfServlet extends HttpServlet {

    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();
        }
        catch(DocumentException e) {
            throw new IOException(e.getMessage());
        }
    }
}

For the full source code, see PdfServlet. You can try the code here: http://demo.itextsupport.com/book/

You wrote in a comment

This demo writes the PDF file into the browser. I want to save the PDF on my hard drive.

This question could be interpreted in two different ways:

  1. You want to write the file to a specific directory on the user's disk drive without any user interaction. This is forbidden! It would be a serious security hazard if a server could force a file to be written anywhere on a user's disk drive.
  2. You want to show a dialog box so that the user can save the PDF on his disk drive in a directory of his choice instead of just showing the PDF in the browser. In this case, please take a closer look at the documentation. You'll see this line: response.setHeader("Content-disposition","attachment;filename="+ "testPDF.pdf"); You can set the Content-disposition to inline if you want the PDF to open in the browser, but in the question, the Content-disposition is set to attachment which triggers a dialog box to open.

See also How to show a Save As dialog for a iText generated PDF?

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • http://demo.itextsupport.com/book/ This demo write pdf into the browser .I want save pdf on my hard drive. – alexis_dia Apr 20 '16 at 14:17
  • I'll update my answer because your question could be interpreted in different ways. (You need to learn how to ask questions more accurately.) – Bruno Lowagie Apr 20 '16 at 14:56
  • Yes, i need as the result standard dialog box that asks the user what he wants to just showing the PDF in the browser or saved to the file system(zip). – alexis_dia Apr 20 '16 at 16:16
  • Thank you very much, it worked. But i havn't know yet, how convert pdf to zip and get standard dialog box that asks the user what he wants to just showing the PDF in the browser or saved to the file system in zip-forma?. – alexis_dia Apr 20 '16 at 18:18
  • There is no *standard* dialog box to do this. You need to know what the user wants *before* sending the response. This would require telepathy or magic. Please ask questions in a more scientific way. For instance: post a new question asking how to serve a ZIP file containing a PDF document. Do not use the comment section to post follow-up questions. Do not expect an answer if you don't accept correct answers. – Bruno Lowagie Apr 20 '16 at 19:26
  • With three down-votes, you are lucky that I even bothered helping you. Please follow the rules of StackOverflow or you might be left with no one wanting to help you. – Bruno Lowagie Apr 20 '16 at 19:29