Saving the report

Questions related to nuBuilder Forte Reports and the Report Builder.

Saving the report

Postby alf1976 » Sat Jun 16, 2018 7:47 pm

i've have created a report that generates a letter quoting prices. it's views fine, but what i want to do is automatically save the pdf on the server and return the file location so i can use this in a table to link the stored document and estimate together. is there an easy way of doing this?
Posts: 75
Joined: Wed Dec 27, 2017 5:28 am

Re: Saving the report

Postby alf1976 » Mon Jul 02, 2018 6:42 am

Posts: 75
Joined: Wed Dec 27, 2017 5:28 am

Re: Saving the report

Postby toms » Mon Jul 02, 2018 11:29 am

Can you share your solution?

Re: Saving the report

Postby alf1976 » Thu Jul 05, 2018 8:31 am

Hi Toms,

This is what i did....

Part 1... Modifying the php file

the report is created and displayed from "nurunpdf.php". This only takes one parameter which is insufficient for my needs. So i duplicated the file and renamed to "nurunpdffile.php". Below is modified code file. Everything before $jsonID = $_GET['i'] and after nuRemoveFiles() is the same as the original file.
i only needed to add a handful of extra lines to the original code.

Step 1) I added an extra two parameters which are retrieved via the URL. i need these to create the link to the file name in my database

Step 2) i use nuID() to create a unique file name for the report. This guarantees no files will be overwritten on the server.

Step 3) added $PDF->Output('/volume1/web/nuBuilder/Reports/'.$fileID . '.pdf', 'F'); (which is my server storage location) before the original output function. This saves the file to the server.
This took a little while to sort. i had to adjust folder permissions to allow the pdf to saved. i would recommended blanking out the $PDF->Output('nureport.pdf', 'I'); until it works correctly because the newly opened window will then display the error messages from the $PDF->Output('/volume1/web/nuBuilder/Reports/'.$fileID . '.pdf', 'F'); function.

Step 4) creates the record in my document table that links the record id and filename together.

Part 2 is below

Code: Select all
$jsonID                  = $_GET['i'];

// step 1 - additional variables required for document link
$type                  = $_GET['type']; // variable to identify type of docuemnt
$recordID                  = $_GET['ID']; // ID of the record

// step -2  use nuID() to generate unique file name
$fileID                  = nuID();
$c                     = "'";

$J                     = nuGetJSONData($jsonID);
$TABLE_ID                  = nuTT();
$JSON                  = json_decode($J);
$LAYOUT                  = json_decode($JSON->sre_layout);
$hashData                  = nuAddToHashList($JSON, 'report');
$hashData['TABLE_ID']         = $TABLE_ID;
$_POST['nuHash']            = $hashData;

$PDF                     = new TCPDF($LAYOUT->orientation, 'mm', $LAYOUT->paper, true, 'UTF-8', false);

$REPORT                  = nuSetPixelsToMM($LAYOUT);
$fl                     = json_decode(nuFontList());

for($i = 0 ; $i < count($fl) ; $i++){
   $fnt               = $fl[$i][0];
   $PDF->AddFont($fnt, '', '', true);
$justID                  = strstr($JSON->parentID, ':');

nuBuildTempTable($JSON->parentID, $TABLE_ID);

$GLOBALS['nu_columns']      = nuAddCriteriaValues($hashData, $TABLE_ID);


nuBuildReport($PDF, $REPORT, $TABLE_ID);
$hashData['nu_pages']        = nuGetTotalPages();
nuReplaceLabelHashVariables($REPORT, $hashData);
nuPrintReport($PDF, $REPORT, $GLOBALS['nu_report'], $JSON);

nuRunQuery("DROP TABLE IF EXISTS $TABLE_ID".'_nu_summary');

// step -3 save pdf output to file
$PDF->Output('/volume1/web/nuBuilder/Reports/'.$fileID . '.pdf', 'F');

$PDF->Output('nureport.pdf', 'I');

// step 4 - generate document record
$filesql = 'INSERT INTO Document (DocumentID, DocumentType, DocumentEntityID, DocumentFileName, DocumentDate)
         VALUES (' .$c. $fileID . $c.',' . $type . ',' . $c.$recordID .$c. ',' .$c. $fileID . '.pdf' .$c.',NOW())';

// run query


Part 2 - the forms javascript

On the form i modified the nuRunReport function from "nuajax.js"

Step 5
changed the last.form_id variable to my report

Step 6
changed the statement to run the modified nurunpdffile adding the extra variable to the url

Step 7
I created Iframe object on the form which runs a browse only form to display the relevant linked documents records
Step 8
Override the browse's nuSelectBrowse(e) to retrieve the filename and then use function to display the stored PDF

Code: Select all
function OnEstimateLetter()
   var p = nuCurrentProperties();
   var current         = nuFORM.getCurrent();
   var last             = $.extend(true, {}, current);

   last.session_id          = window.nuSESSION;
   last.call_type         = 'runreport';

   // step 5
   last.form_id         = 'EstLetter';  // report name
   last.hash             = nuHashFromEditForm();
   var successCallback = function(data,textStatus,jqXHR)
      var fm          = data;
         // Step 6
         var pdfUrl   = 'nurunpdffile.php?i=' + + '&type=0&ID='+p.record_id; // open report added extra data variables
Posts: 75
Joined: Wed Dec 27, 2017 5:28 am

Re: Saving the report

Postby admin » Thu Jul 05, 2018 8:52 am

Site Admin
Posts: 3070
Joined: Mon Jun 15, 2009 9:53 am

Re: Saving the report

Postby toms » Sat Jul 07, 2018 1:05 am

Thanks a lot for sharing the code. I will test it out and let you know the results.

Re: Saving the report

Postby admin » Wed Jul 11, 2018 10:56 am

Site Admin
Posts: 3070
Joined: Mon Jun 15, 2009 9:53 am

Return to Reports