0

I have written a code for pdf generation and it is working fine but now I to generate a pdf file in secured mode.

Here is my code for Secured mode

     try {
        HttpServletResponse response = ServletActionContext.getResponse();
        PDFGenerator pdf = new PDFGenerator();

        PDFGenerator generator=new PDFGenerator();


    /*    byte[] bytes = null;
        bytes = (generator.generatepdf(sosValues.getCmaId(), null)).toByteArray();

        //bytes = buffer.toByteArray();
        response.setContentLength(bytes.length);

        if (bytes != null) {
            bis = new ByteArrayInputStream(bytes);
        }*/

        ByteArrayOutputStream baos=generator.generatepdf(sosValues.getCmaId(), null);
        bis = new ByteArrayInputStream(baos.toByteArray());

        PdfReader pdfReader=new PdfReader(bis);

        PdfStamper pdfStamper=new PdfStamper(pdfReader, baos);
        pdfStamper.setEncryption(null,null, PdfWriter.HideToolbar, PdfWriter.STRENGTH40BITS);
    pdfStamper.setEncryption("Hello".getBytes(), "World".getBytes(), PdfWriter.AllowPrinting
                | PdfWriter.AllowCopy, PdfWriter.STRENGTH40BITS);
        pdfStamper.close();




        baos.close();






    } catch (Exception e) {
        e.printStackTrace();
    }

While debugging I was getting an exception at this line pdfStamper.setEncryption(null,null, PdfWriter.HideToolbar, PdfWriter.STRENGTH40BITS);

Exception in browser was:

The server encountered an internal error that prevented it from fulfilling this request.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
jeetZ
  • 65
  • 2
  • 11
  • 1
    You should at least say what you are using to create the PDF (iText, appearently), tag it, and add the underlying error, not that high level nonsense, by looking at the server log and not in the browser. – Andrea Ligios May 05 '14 at 08:17

1 Answers1

0

PdfWriter.HideToolbar is a viewer preference, not a permission.

This is the list of permissions:

  • PdfWriter.ALLOW_PRINTING
  • PdfWriter.ALLOW_MODIFY_CONTENTS
  • PdfWriter.ALLOW_COPY
  • PdfWriter.ALLOW_MODIFY_ANNOTATIONS
  • PdfWriter.ALLOW_FILL_IN
  • PdfWriter.ALLOW_SCREEN_READERS
  • PdfWriter.ALLOW_ASSEMBLY
  • PdfWriter.ALLOW_DEGRADED_PRINTING

Moreover: hiding the toolbar in the hope to secure a PDF is wrong. Please read my answer to How to disable download option of pdf file in c# ?

Even using encryption to avoid printing may not be the best of ideas. See How to protect a PDF with a username and password?

However, this isn't what causes your problem. The internal error is caused by the strange way you're using the ByteArrayOutputStream. You generate a PDF in memory in the generatepdf() method. You didn't share that method, but:

  • if you're closing that stream, you get the exception because you're trying to add new bytes to it with your stamper object. You can't add extra bytes to a closed OutputStream.
  • if you're not closing that stream, your PDF isn't completer and you'll get an exception when PdfReader tries to read the (unfinished) PDF.

Furthermore, it's very strange that you would first create the PDF, and then read that PDF to encrypt it. Why not encrypt it right away? That saves you CPU-time.

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