How to disable options as printing in generated PD

2019-08-09 02:35发布

问题:

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 program which visualize it (web browser,adobe etc.).

回答1:

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.