Generate pdf file in Secured mode

穿精又带淫゛_ 提交于 2019-12-13 06:24:03

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/23466710/generate-pdf-file-in-secured-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!