Welcome to the nuBuilder Forums!
Register and log in to access exclusive forums and content available only to registered users.
Register and log in to access exclusive forums and content available only to registered users.
Saving the report
-
- Posts: 75
- Joined: Tue Dec 26, 2017 8:58 pm
Saving the report
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: 785
- Joined: Sun Oct 14, 2018 11:25 am
-
- Posts: 75
- Joined: Tue Dec 26, 2017 8:58 pm
Re: Saving the report
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
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 window.open function to display the stored PDF
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;
$GLOBALS['TABLE_ID'] = $TABLE_ID;
$_POST['nuHash'] = $hashData;
$PDF = new TCPDF($LAYOUT->orientation, 'mm', $LAYOUT->paper, true, 'UTF-8', false);
$PDF->SetAutoPageBreak(true);
$REPORT = nuSetPixelsToMM($LAYOUT);
$PDF->SetMargins(1,1,1);
$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);
nuRunQuery("ALTER TABLE $TABLE_ID ADD `nu__id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST");
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");
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
nuRunQuery($filesql);
nuRemoveFiles();
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 window.open 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;
if(!nuDisplayError(fm))
{
// Step 6
var pdfUrl = 'nurunpdffile.php?i=' + fm.id + '&type=0&ID='+p.record_id; // open report added extra data variables
window.open(pdfUrl);
}
}
nuAjax(last,successCallback);
}
-
- Posts: 785
- Joined: Sun Oct 14, 2018 11:25 am
Re: Saving the report
Thanks a lot for sharing the code. I will test it out and let you know the results.