-1

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-format?

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());
        }
    }
}
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
alexis_dia
  • 156
  • 3
  • 13
  • 1
    You really need to learn how StackOverflow works. Your behavior on StackOverflow doesn't make people want to answer your questions. See my comment on your previous question. – Bruno Lowagie Apr 20 '16 at 19:35

1 Answers1

1

Once more, I have to refer you to the official documentation. For some reason you don't seem to be able to access the resources that are available on that site.

Take a look at the HelloZip example:

// creating a zip file with different PDF documents
ZipOutputStream zip =
    new ZipOutputStream(new FileOutputStream(RESULT));
for (int i = 1; i <= 3; i++) {
    ZipEntry entry = new ZipEntry("hello_" + i + ".pdf");
    zip.putNextEntry(entry);

    // step 1
    Document document = new Document();
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, zip);
    writer.setCloseStream(false);
    // step 3
    document.open();
    // step 4
    document.add(new Paragraph("Hello " + i));
    // step 5
    document.close();

    zip.closeEntry();
}
zip.close();

In this example, we create three PDF files that are added to a single ZIP file. In your case, you want to create a ZIP file in memory that contains only one ZIP file. That's easy:

// Creating a ByteArrayOutputStream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// creating a zip outputstream stream
ZipOutputStream zip = new ZipOutputStream(baos);
// prepare entry
ZipEntry entry = new ZipEntry("my_document.pdf");
zip.putNextEntry(entry);
// create PDF
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, zip);
writer.setCloseStream(false);
document.open();
document.add(new Paragraph("Hello " + i));
document.close();
zip.closeEntry();
zip.close();

Now you can sent the bytes stored in the baos to your response object as explained in my answer to your previous question: Why doesn'n create pdf-documents in java servlet?

OutputStream os = response.getOutputStream();
baos.writeTo(os);

This answers the first part of your question "How convert pdf to zip" and I hope that this time you'll accept my answer. I already answered one question for you and although you said that my answer worked for you, you didn't accept my answer. (Why would anyone want to provide good answers if the person getting the answer doesn't accept correct answers?)

As for the second part of your question 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-format, that part is unanswerable. There is no standard box to do this. The server gets a request and sends a response. You can only send one single response so there's no way to send a PDF to show inline as well as a ZIP to open as an attachment at the same time so that the user can choose through a dialog box.

You need to give the user the option to choose for an inline PDF, a PDF as an atachment or a PDF in a ZIP file before sending the request to the server. There is nothing standard about that!

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165