For this example, set your onclick event to:
Code: Select all
pui.download({ "id": "itemrpt", "inline": true })
The "id" can be anything you want. It's just a key for the exit program to look for. In this case, when the exit program sees an id of "itemrpt" we want it to call a program that prints an item report to PDF format and download it. The "inline: true" tells it to open the PDF in a browser window (as opposed to initiating a download).
For security purposes, PUI will not simply download any document that you ask for. Instead, it calls an exit program, which is an RPG program that you write. It must be named PUIDNLEXIT and must be located in the library you installed Profound UI into (PROFOUNDUI is the library name by default, but can be changed when you install PUI)
The exit program can be used to do more than just look for a path name and validate it, however. It can also actually change the path name, and even generate an IFS document on the fly... which is what we want to do here. So it'll look for "itemrpt", and if found, it'll set up a temporary filename in the IFS and change the path name to that, and generate a PDF. It'll also delete teh PDF after the download is complete.
Code: Select all
**********************************************************************************************
* *
* Description: Profound UI File Download Exit Program *
* *
* Compile as PUIDNLEXIT in PROFOUNDUI product library using CRTBNDRPG. *
* *
* Return 1 in 'Allow' parameter to allow the upload, any other value will *
* prevent the upload. *
* *
**********************************************************************************************
H DFTACTGRP(*NO) ACTGRP(*CALLER)
D InputData_t ds qualified
D based(Template)
D fileid 640a varying
D userid 10a
D ipAddr 15a
D inline 1n
D Main PR ExtPgm('PUIDNLEXIT')
D timingFlag 10i 0 const
D inputData likeds(InputData_t) const
D stmfDir 640a varying
D stmfName 256a varying
D attName 256a varying
D contentType 255a varying
D allow 5i 0
D Main PI
D timingFlag 10i 0 const
D inputData likeds(InputData_t) const
D stmfDir 640a varying
D stmfName 256a varying
D attName 256a varying
D contentType 255a varying
D allow 5i 0
D RunItemRpt PR
D tempfile 50a varying const
D tmpnam PR * extproc('_C_IFS_tmpnam')
D string 39A options(*omit)
D unlink PR 10I 0 ExtProc('unlink')
D path * Value options(*string)
D CmdExc PR ExtPgm('QCMDEXC')
D cmd 32702a const options(*varsize)
D len 15p 5 const
/free
allow = 0;
if timingFlag=0;
select;
when inputData.fileId = 'itemrpt';
stmfDir = '/tmp';
stmfName = %subst(%str(tmpnam(*omit)):6) + '.pdf';
attName = 'itemReport.pdf';
contentType = 'application/pdf';
RunItemRpt( stmfDir + '/' + stmfName );
allow = 1;
// maybe have other 'when' situations here
// to have other IDs sent by different
// applications?
endsl;
endif;
if timingFlag=1 and inputData.fileId='itemrpt';
unlink( stmfDir + '/' + stmfName );
endif;
*inlr = *on;
/end-free
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* RunItemRpt(): Run the RPG program that generates
* an item report
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++
P RunItemRpt B
D PI
D tempfile 50a varying const
D ITM001R PR ExtPgm('ITM001R')
D cmd s 500a varying
/free
// Run the Item Report and override the output to
// a PDF file in the temporary name
monitor;
cmd = 'ADDLIBLE SKTEST *LAST';
CmdExc(cmd: %len(cmd));
on-error;
endmon;
cmd = 'OVRPRTF FILE(QSYSPRT) DEVTYPE(*AFPDS) WSCST(*PDF) +
OVRSCOPE(*CALLLVL) +
TOSTMF(''' + TempFile + ''')';
CmdExc(cmd: %len(cmd));
ITM001R();
cmd = 'DLTOVR FILE(QSYSPRT) LVL(*)';
CmdExc(cmd: %len(cmd));
/end-free
P E
The tmpnam() call is an IBM-provided API to calculate a unique temporary file name. So this will just generate a name that's not in use. The tmpnam() will return a path name beginning with /tmp/Qxxxx -- but since PUI wants the directory name in a separate variable, we use %SUBST to remove the first 5 characters to remove "/tmp/" from the start.
So /tmp goes in the directory variable, and the rest of the temporary name goes in the file name variable.
The "attName" is the filename that the user will see. Instead of reporting the temporary filename (that we actually use) to the browser, I'm specifying "itemReport.pdf" so it looks a bit nicer to the user.
The "runItemReport" procedure is just calling an exiting RPG program, but using an override to force the output into the temporary filename that we calculated previously. It's using IBM's "transformation services" licensed program that needs to be installed if you haven't done so already. "transformation services" is a no-charge item included with the operating system if you are at V6R1 or higher.
The PUIDNLEXIT exit program will be called twice. Once before the download (so it can generate the PDF) and once after teh download is complete (so it can delete the temporary IFS object). The timingFlag variable will be set to 0 when it is called before the download, and 1 when it is called after the download, so this program checks for that parameter, and uses it to decide whether to create or delete the temporary IFS file.
Hope that helps.