本文介绍了在安全模式下生成pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个pdf生成代码,它工作正常,但现在我以安全模式生成一个pdf文件。

I have written a code for pdf generation and it is working fine but now I to generate a pdf file in 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();
    }

调试时我在此行收到异常 pdfStamper.setEncryption(null,null,PdfWriter.HideToolbar,PdfWriter.STRENGTH40BITS);

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

浏览器中的异常是:


推荐答案

PdfWriter.HideToolbar 查看者偏好,而不是权限

这是权限列表:


  • 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

  • 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

此外:隐藏工具希望保护 PDF的栏是错误的。请阅读我对

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?

但是,这不是导致问题的原因。内部错误是由您使用 ByteArrayOutputStream 的奇怪方式引起的。您可以在 generatepdf()方法中在内存中生成PDF。你没有分享那个方法,但是:

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:


  • 如果你关闭那个流,你会得到例外,因为你正试图使用压模对象为其添加新字节。您不能将额外的字节添加到已关闭的 OutputStream

  • 如果您没有关闭该流,则您的PDF不是完成时,当 PdfReader 尝试读取(未完成的)PDF时,你会得到一个例外。

  • 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.

此外,您首先创建PDF,然后读取该PDF以加密它是非常奇怪的。为什么不马上加密?这可以节省你的CPU时间。

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.

这篇关于在安全模式下生成pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 19:35