How to disable options as printing in generated PDF file?

后端 未结 1 1012
失恋的感觉
失恋的感觉 2020-12-20 05:04

When I generate a PDF with jasper reports I don\'t want the user to be able to print it.

Is there some option to make it from the code or does it only depend on the

相关标签:
1条回答
  • 2020-12-20 06:08

    You can achive this both by using jrxml properties or setting values to the SimplePdfExporterConfiguration if you are exporting from java.

    To protected your pdf document (hence in this case disallow printing), first thing you need to do is to encrypt it and be sure that you have nessary libraries for encryption in classpath see How to configure PDF encryption in JasperReports Server 5.6.1

    jrxml properties

    <property name="net.sf.jasperreports.export.pdf.encrypted" value="true"/>
    <property name="net.sf.jasperreports.export.pdf.128.bit.key" value="true"/>
    <property name="net.sf.jasperreports.export.pdf.owner.password" value="12345"/>
    

    or

    java code

    SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
    configuration.setEncrypted(true);
    configuration.set128BitKey(true);
    configuration.setOwnerPassword("1234"); 
    

    Note we are setting owner password not user password, hence user will be allowed to open without.

    Now set the user permissions

    net.sf.jasperreports.export.pdf.permissions.allowed

    In your case I guess you only like to allow screen readers, if you like to also allow COPY or other actions (see link above) add these with | to your properties

    jrxml property

    <property name="net.sf.jasperreports.export.pdf.permissions.allowed" value="SCREENREADERS"/>
    

    or

    java code

    configuration.setPermissions(PdfWriter.ALLOW_SCREENREADERS);
    

    Notice: it is up to the reader/application to respect the permission, hence a developer can always open and do what they want with any PDF document. As example iText contains a flag unethicalreading, if you set it to true, you will be able to have owner access to these documents without knowing the password.

    0 讨论(0)
提交回复
热议问题