Forum Samples, Tips and Tricks

Updated pince.php Class to support multiple doc processing

matt_d_rat
The company which I work for have been looking at introducing PrinceXML to handle the PDF conversion of our invoices we generate in HTML. This has worked very well. The only problem I found was that we also have a need to export multiple invoices as one single PDF. Now having played about with prince from the command line I knew that this was possible, but the prince class as it stands didn't support this due to the way it creates the path and arguments for the command line.

I have made a simple change to the prince.php class which allows for it to now support the passing of multiple docs as an array, so I thought I would share my changes with everyone on here so that we can all benefit:

Replace the convert_file() function in prince.php class with the following:

    // Convert an XML or HTML file to a PDF file.
    // The name of the output PDF file will be the same as the name of the
    // input file but with an extension of ".pdf".
    // xmlPath: The filename of the input XML or HTML document. Modified by Matt Fairbrass to support passing an array of input XML or HTML documents.
    // msgs: An optional array in which to return error and warning messages.
    // Returns true if a PDF file was generated successfully.
    public function convert_file($xmlPath, &$msgs = array())
    {
	$pathAndArgs = $this->getCommandLine();
		
		// Modified by Matt Fairbrass (www.aplaceformyhead.co.uk) to support processing multiple urls from an array.
		if(is_array($xmlPath)) {
			foreach($xmlPath as $path) {
				$pathAndArgs .= '"'.$path.'" ';
			}
			$pathAndArgs = rtrim($pathAndArgs); // remove trailing white space
		}
		else {
			$pathAndArgs .= '"' . $xmlPath . '"';
		}

		return $this->convert_internal_file_to_file($pathAndArgs, $msgs);
    }


and finally replace the convert_file_to_file() function in prince.php class with the following:

    // Convert an XML or HTML file to a PDF file.
    // xmlPath: The filename of the input XML or HTML document. Modified by Matt Fairbrass to support passing an array of input XML or HTML documents.
    // pdfPath: The filename of the output PDF file.
    // msgs: An optional array in which to return error and warning messages.
    // Returns true if a PDF file was generated successfully.
    public function convert_file_to_file($xmlPath, $pdfPath, &$msgs = array())
    {
	$pathAndArgs = $this->getCommandLine();
		
		// Modified by Matt Fairbrass (www.aplaceformyhead.co.uk) to support processing multiple urls from an array.
		if(is_array($xmlPath)) {
			foreach($xmlPath as $path) {
				$pathAndArgs .= '"'.$path.'" ';
			}
			$pathAndArgs .= ' "'.$pdfPath.'"';
		}
		else {
			$pathAndArgs .= '"' . $xmlPath . '" "' . $pdfPath . '"';
		}
	
        return $this->convert_internal_file_to_file($pathAndArgs, $msgs);
    }


This now allows you to call either of these functions with either a single string to a document, or pass an array of documents to process multiple and output as a single PDF. For example:

$array = array(0=>'http://www.google.com', 1=>'http://www.aplaceformyhead.co.uk');
$pdfPath = '/path/to/pdf/output.pdf';

$prince = new Prince('/path/to/prince');
$prince->convert_file_to_file($array, $pdfPath);


Hope you guys find it helpful :-D
mikeday
Nice work Matt! :D
matt_d_rat
No problem! Always a pleasure to give back to the community, especially after receiving fantastic support. :D