我已经将我的报告上传到JasperServer上,在那里我正在安排报告,并使用作业rest以电子邮件的形式将pdf作为附件发送给用户。一切都很完美,但是我们也需要对pdf进行加密。我读过wiki主题并且能够加密pdf。
但是我们希望密码是动态的,并且对每个用户来说都是不同的(比如他们的电话号码和出生日期的组合)。链接中描述的示例将密码指定为jrxml中的report属性。
<property name="net.sf.jasperreports.export.pdf.user.password" value="123456"/>
<property name="net.sf.jasperreports.export.pdf.owner.password" value="123456"/>
密码被指定为字符串,对于从这个jrxml生成的每一个pdf来说都是相似的。
我试过这样的方法
<property name="net.sf.jasperreports.export.pdf.user.password" value="{$F{dateOfBirth}}"/>
其中$F{ dateOfBirth }是运行查询的用户的dateOfBirth。但是它没有输入字段值,而是将其视为一个字符串,并设置密码to=“{$F{dateOfBirth}”。
我该怎么做呢?他们有没有办法让我为每个用户设置不同的密码?
注意:数据源是为jasperserver上的报表配置的。在作业执行api调用中,Jasperserver执行查询,填充报告,导出为pdf,并将其作为电子邮件发送给用户。
发布于 2016-11-30 07:06:44
正如一条评论所提到的,只需使用Java即可。
下面是一个示例,我将如何编写这个代码(这并不完美,但我认为您会理解的):
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;
}
}
将这个小片段打包到一个jar中,并将其添加到Studio构建路径中,并将其上传到Jaspersoft服务器。
在您的报告中,概要说明了在scriptlet上进行->“codesnippet”,这个脚本的类是GetBirthdayScriptlet (这是codesnippet类)。
要在报表中使用的表达式是:
$P{>>scriptlet-name<<_SCRIPTLET}.getBirthday("email@example.com")
不要输入字符串,只需使用参数即可。
另外,也许可以考虑使用参数LoggedInUserEmailAddress中内置的Jaspersoft
如果你想对现场报告进行加密,这会有所帮助。
https://stackoverflow.com/questions/40810417
复制