3

I've uploaded my reports on JasperServer where I'm scheduling the reports and send the pdf as attachments in emails to the users using the jobs rest api. Everything works perfectly, however we also need the pdf's to be encrypted. I've read the wiki topic and was able to encrypt the pdf.

But we want the passwords to be dynamic and be different for every user(for exmaple some combination of their phone numbers and date of births). The example described in the link specifies the password as a report property in the jrxml.

<property name="net.sf.jasperreports.export.pdf.user.password" value="123456"/>
<property name="net.sf.jasperreports.export.pdf.owner.password" value="123456"/> 

The password is specified as a string and is similar for every pdf generated from this jrxml.

I tried something like this

<property name="net.sf.jasperreports.export.pdf.user.password" value="{$F{dateOfBirth}}"/>

where $F{dateOfBirth} is the dateOfBirth of the user for which the query is being run. But instead of putting in the field value, it just considers it a string and sets the password to="{$F{dateOfBirth}}"

How do I go along with this? Is their any way for me to set different passwords for every user?

NOTE:The datasource is configured for the report on the jasperserver. On the job execution api call, Jasperserver executed the query, fills the report, exports as pdf and sends it as email to the user.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
user7153719
  • 41
  • 1
  • 5
  • @DaveJarvis Thanks for the answer Davis! Although as I've said above, I cannot set values using Java code because the report is run by the JasperServer on job execution rest api calls (http://community.jaspersoft.com/documentation/jasperreports-server-ultimate-guide/v55/report-scheduling-api) and uses the datasource that's been configuered for the report. Also regarding the propertyExpressions, I think these are for setting protperties for textFields and such elements where as pdf encryption properties are the report properties. Anyways I'll try it out and let you know. Cheers! – user7153719 Nov 26 '16 at 10:41
  • @Dave I tried the propertyExpressions but the Jasperserver doesn't accept the jrxml. It doesn't work. Isn't there anyone who has used dynamic passwords for their jrxml? – user7153719 Nov 26 '16 at 17:23
  • 1
    Do you have the opportunity to perform [post-processing](http://stackoverflow.com/q/25681239/59087) on the PDF after it is generated but before it is sent over the wire? – Dave Jarvis Nov 26 '16 at 19:30
  • No, the job execution rest api executes the report, exports it as pdf and sends it as an attachment. – user7153719 Nov 26 '16 at 19:52
  • @user7153719 You can't use expression with parameter (or field, variable) in `property`. The only chance to use expression is to use `propertyExpression` element, but it can't be used at *jasperReport* section. You can use *Java API* – Alex K Nov 26 '16 at 20:17
  • @Dave I've tried this with parameters as well. It doesn't work either. Anyways, I had asked another JasperReports question a while back which I got no response to. Could you please have a look at the (http://stackoverflow.com/questions/40577448/sending-html-content-in-the-email-body-in-jasperservers-scheduled-reports) and see if you can help. Thanks for your time! – user7153719 Nov 26 '16 at 20:41
  • @Alex Could you please elaborate how do I use Java API to help with the issue? – user7153719 Nov 26 '16 at 20:45
  • 2
    1) You can modify the code of *JasperReports* library 2) You can modify the code of *JR Server* 3) You can add post handler – Alex K Nov 26 '16 at 20:47
  • @Alex AFAIK jasperserver uses compiled jars to provide the functionalities. How do you suggest I modify the code? – user7153719 Nov 26 '16 at 21:01
  • [Source code is here](http://code.jaspersoft.com/svn/repos/jasperserver/) & [Build guide](https://docs.tibco.com/pub/js-rptsrvr-amxbpm/6.1.1/doc/pdf/TIB_js-rptsrvr-amxbpm_6.1.1_Source_Build.pdf.pdf) :) – Alex K Nov 26 '16 at 21:05
  • It prompts for username/password. Requires authentication. – user7153719 Nov 26 '16 at 21:09

2 Answers2

1

Add the following code in your Java code.

JasperPrint print = JasperFillManager.fillReport(jasper, parameters, beanColDataSource2); print.setProperty("net.sf.jasperreports.export.pdf.user.password", "jasper123");

Add in JRXML.

 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.permissions.allowed" value="PRINTING"
Pramin Senapati
  • 136
  • 1
  • 4
0

As one comment mentioned, just use Java.

Here is an example, how I would code this (it's not perfect, but I think you will get it):

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import net.sf.jasperreports.engine.JRDefaultScriptlet;
import net.sf.jasperreports.engine.JRScriptletException;
import net.sf.jasperreports.engine.fill.JRFillParameter;

public class GetBirthdayScriptlet extends JRDefaultScriptlet {

    private Connection conn;

    private Connection getConnection() throws JRScriptletException {
        if (conn == null) {
            if (getParameterValue(JRFillParameter.REPORT_CONNECTION) != null) {
                conn = (Connection) (getParameterValue(JRFillParameter.REPORT_CONNECTION));
            } else {
                throw new RuntimeException("No db-connection configured in the report!");
            }
        }
        return conn;
    }

    public String getBirthday(String email) throws JRScriptletException, SQLException {
        ResultSet result = null;
        String resultString = null;
        CallableStatement stmt = getConnection().prepareCall("select birthday from birthday_table where email = " + email);
        stmt.executeUpdate();
        result = stmt.getResultSet();
        if(result.next()){
            result.getString(1);
        }
        return resultString;
    }
}

Pack this little snippet into a jar and add it to your Studio Build Path and also upload it to your Jaspersoft Server.

In your report outline rightlick on Scriptlets -> "Create Scriptlet" The class of the scriptlet is GetBirthdayScriptlet (this is the codesnippet-class).

The expression you want to use in your report is:

$P{>>scriptlet-name<<_SCRIPTLET}.getBirthday("email@example.com")

Instead of entering the String, just use the parameter.

Also, maybe think of using the Jaspersoft Built In Parameter LoggedInUserEmailAddress

This helps if you want live-reports to be encrypted.

Markus Deindl
  • 318
  • 1
  • 8
  • Thanks for the answer! Although I've also tried using the scriptlets. The problem I had was where to use the password parameter value i.e where to put the **$P{>>scriptlet-name<<_SCRIPTLET}.getBirthday("email@example.com")** in the jrxml. I tried something like this **$P{>>scriptlet-name<<_SCRIPTLET}.getBirthday("email@example.com")** but jasperserver did not upload the jrxml then. Could you please tell me how to set the password-property value to the scriptlet parameter? – user7153719 Nov 30 '16 at 19:21
  • Furthermore, the user specifies the query parameters via the job execution rest api. So the _**email@example.com**_ in **$P{>>scriptlet-name<<_SCRIPTLET}.getBirthday("email@example.com")** would actually be specified by the user in the API calls. – user7153719 Nov 30 '16 at 19:29
  • If you want to use expressions in properties just use the `<![CDATA[$P{Password}]]>` – Markus Deindl Dec 01 '16 at 10:43
  • You will have to do in in an element since the compiler doesn't created any parameters before setting the report properties. This usually works fine, **but** as I just found out, export.pdf.user.password is one of the rare cases, where this doesn't work. Now I don't see a way to solve this problem... – Markus Deindl Dec 01 '16 at 10:46
  • Thanks for the help anyways!! I've posted another question http://stackoverflow.com/questions/40577448/sending-html-content-in-the-email-body-in-jasperservers-scheduled-reports to which I got no answer to yet. Could you please have a look at it and see if you could help. – user7153719 Dec 05 '16 at 05:13