#1 2017-01-18 15:22:11

srpopovic
Member
Registered: 2017-01-18

Generated pdf report file name configuration

Hi Guys:

I am calling following url to generate a report in pdf format and am passing in login parameters for the configured user from \etc\misc\httpauthexecute.cf,
report id and report input parameter p_aplctn_id:

https://reportserverUrl/reportserver/reportserver/httpauthexport?id=123456&p_p_aplctn_id=12345678&format=pdf&user=embeduser&password=embeduserpw&key=mykey

The created pdf report file is generated in a file name format: 201701180925_ReportName - ReportName.pdf and saved in Downloads folder.

Can the report generator be configured to include the passed-in report parameter p_aplctn_id=12345678 in the file name?

It would be great to have a file name like 201701180925_12345678_ReportName - ReportName.pdf.

This would uniquely identify pdf report file generated for the passed-in p_aplctn_id=12345678.

Thanks in advance for your response,
Srdjan

Offline

#2 2017-01-22 14:42:22

karolina
Member
Registered: 2014-08-09

Re: Generated pdf report file name configuration

Hi Srdjan,

Yes, it is possible - by extending ReportServer functionalities using hooks. (To get more info about hooks see the Script Guide chapter Tapping into ReportServer)

In your case you need to register a hook using the following code:

import net.datenwerke.rs.core.server.reportexport.hooks.ReportExportAdjustHttpHeaderHook
import net.datenwerke.rs.core.service.parameters.entities.ParameterInstance
import net.datenwerke.rs.core.service.reportmanager.engine.CompiledReport
import net.datenwerke.rs.core.service.reportmanager.entities.reports.Report
import net.datenwerke.rs.core.service.reportmanager.parameters.ParameterSet;
import net.datenwerke.rs.core.service.reportmanager.parameters.ParameterSetFactory;

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import java.text.SimpleDateFormat

/**
 * The hook adjusts http header so the exported report filename contains a value of one of the report's parameters.
 *
 * First it gets the name of the parameter which value is to be used in the exported file name. The key of this
 * parameter is p_aplctn_id. In this way a report designer may decide about the
 * parameter value that will be included in the filename and for every report this parameter value may be different.
 *
 * If the parameter value is found and is not null, the export filename is created as defined in the hook.
 * Otherwise it is created in the default way.
 *
 */

def HOOK_NAME = "ADJUST_FILE_NAME"
def parameterSetFactory = GLOBALS.getInstance(ParameterSetFactory.class)

def makeExportFilename = { Report report ->

    def parameterSet = parameterSetFactory.create(report);
    parameterSet.addAll(report.getParameterInstances());
    def fnParamValue = parameterSet.getParameterMapSimple().get("p_aplctn_id");

    if (fnParamValue == null || fnParamValue == ""){
        
        return null
    } else {
        return  fnParamValue
    }
    
}

def callback = [

        adjustHeaders : { Report report, CompiledReport executedReport, HttpServletRequest req, HttpServletResponse resp ->

            if(makeExportFilename(report) != null) {

                def parentName = report.getParent().getName().replaceAll("[^a-zA-ZüÜöÖäÄß\\(\\)\\[\\] \\-\\.0-9]+","_")

                def reportName = report.getName().replaceAll("[^a-zA-ZüÜöÖäÄß\\(\\)\\[\\] \\-\\.0-9]+","_")

                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss")

                Date date = new Date()

                def formatedDate = sdf.format(date)

                def filename = formatedDate + "_" + "${makeExportFilename(report)}" + "_" + parentName+ " - " + reportName + "." + executedReport.getFileExtension()

                resp.setHeader("Content-Disposition", "attachment; filename =\"" + filename + "\"")
            }

        }

] as ReportExportAdjustHttpHeaderHook

GLOBALS.services.callbackRegistry.attachHook(HOOK_NAME, ReportExportAdjustHttpHeaderHook.class, callback)

Detailed steps:
1. In the ReportServer filesystem, under the 'bin' folder, create a new file called name_hook.groovy
2. Set the file content type to text/groovy
3. Save the file
4. Then choose 'Edit file' and paste the above code there and press 'Apply'
5. Open the ReportServer terminal (press: Ctrl + Alt + T)
6. Go to the folder with the name_hook.groovy file (type: cd fileserver/bin)
7. Register the hook by typing: exec name_hook.groovy

If all goes well, you should see ADJUST_FILE_NAME displayed in the terminal

Then add a text parameter with the key 'p_aplctn_id' to any report that you'd like to be affected by the hook.

If there is no p_aplctn_id parameter in a report, the report file download name will be default.

Note that if you restart ReportServer, the hook will be detached. To prevent this, place the hook script in the on.startup folder (then the hook will be re-attached at ReportServer startup).

Hope it helps
Karolina

Offline

#3 2017-01-24 14:37:23

srpopovic
Member
Registered: 2017-01-18

Re: Generated pdf report file name configuration

Yes, I've added the groovy script and it works as expected.

Now the pdf file is generated with customized name:

20170124101411_98989898_ReportName - ReportName.pdf

Thank you Karolina

Offline

#4 2023-02-15 19:31:22

jv2023411
Member
Registered: 2023-02-15

Re: Generated pdf report file name configuration

The export hook mentioned above works great for the report export option.

Is there a similar hook that can be used to rename the report when using any of the "Send To" options or when creating scheduled reports so they can take advantage of "adjusting the file name" of the report using the report parameters defined in a report?

I have created a new Menu item hook that will creates a new "Send to zipped" menu item under the "Sent to" item list that will send mail immediately with the Adjusted File Name, however, I would prefer to have a generic ADJUST_FILE_NAME hook that works for all Send To options a user can select using the existing "Send to" menu list options at report runtime and that the scheduled reports would take advantage of.

Thank you,
Jay

Offline

#5 2023-02-23 11:26:15

eduardo
Administrator
Registered: 2016-11-01
Website

Re: Generated pdf report file name configuration

Hi Jay,

currently, there is no hook for all options. We will look into that in some future version.

Regards,
Eduardo

Offline

Board footer

Powered by FluxBB