Page 1 of 1

Saving the report

Posted: Sat Jun 16, 2018 12:17 pm
by alf1976
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?

Re: Saving the report

Posted: Sun Jul 01, 2018 11:12 pm
by alf1976
Sorted.

Re: Saving the report

Posted: Mon Jul 02, 2018 3:59 am
by toms
Can you share your solution?

Re: Saving the report

Posted: Thu Jul 05, 2018 1:01 am
by alf1976
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;
$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);
  
}

Re: Saving the report

Posted: Thu Jul 05, 2018 1:22 am
by admin
.

Re: Saving the report

Posted: Fri Jul 06, 2018 5:35 pm
by toms
Thanks a lot for sharing the code. I will test it out and let you know the results.

Re: Saving the report

Posted: Wed Jul 11, 2018 3:26 am
by admin
.