生成受密码保护的pdf。如何为每个用户获取唯一密码?

问题描述 投票:2回答:2

我已经在JasperServer上传了我的报告,我正在安排报告,并使用jobs rest api将pdf作为电子邮件中的附件发送给用户。一切都很完美,但我们还需要加密pdf。我已经阅读了wiki topic并且能够加密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。但它不是放入字段值,而是将其视为字符串并将密码设置为=“{$ F {dateOfBirth}}”

我该怎么做?他们以任何方式为每个用户设置不同的密码吗?

注意:为jasperserver上的报告配置了数据源。在作业执行api调用中,Jasperserver执行查询,填写报告,导出为pdf并将其作为电子邮件发送给用户。

jasper-reports password-protection jasperserver export-to-pdf
2个回答
0
投票

正如一条评论所提到的,只需使用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 Build Path中,并将其上传到Jaspersoft Server。

在您的报告大纲中右键单击Scriptlets - >“Create Scriptlet”Scriptlet的类是GetBirthdayScriptlet(这是codesnippet-class)。

您要在报告中使用的表达式是:

$P{>>scriptlet-name<<_SCRIPTLET}.getBirthday("[email protected]")

不要输入String,只需使用参数即可。

另外,也许可以考虑使用Jaspersoft内置参数LoggedInUserEmailAddress

如果您希望加密实时报告,这会有所帮助。


0
投票

在Java代码中添加以下代码。

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

添加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"
© www.soinside.com 2019 - 2024. All rights reserved.