Forum How do I...?

is it possible to combine multiple files through php?

alyda
Hi, i'm still having trouble working out my php issues, i understand that php is a side-step to writing command-line functions (which took me quite a while to get used to and really understand); i definitely have the hang of it in terminal, but now i need to combine multiple XML files into a single pdf by way of php. i know there isn't a pre-defined function to do this, but is it possible to work around it because the php is essentially writing the same commands that i would in terminal?
mikeday
Yes, you can edit the prince.php file to add a new function that allows you to specify multiple input files. I would suggest copying the convert_file_to_file method and adding more arguments, or perhaps an array of input filenames, which get added to the $pathAndArgs variable. Add a "-o" before the $pdfPath as well so it's clear which arguments are input filenames and which is the output filename.
alyda
ok, after thinking about it, here's the code i came up with, but i have no way to test this since i still can't get php to run prince (see http://www.princexml.com/bb/viewtopic.php?p=4204#4204 )


	
    // Convert multiple XML or HTML files to a PDF file.
    // xmlPath: The array of filenames of the 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_files_to_pdf($xmlPaths, $pdfPath, &$msgs = array())
    {
//	set new variable to hold file names as string separated by spaces
	$xmlPath = "";	
//	get length of array	
	$numFiles = count($xmlPaths);
	for (i=0 ; i <= $numFiles; i++ ) {
		$xmlPath .= array_shift($xmlPaths;)." ";
	}
	
	$pathAndArgs = $this->getCommandLine();
	$pathAndArgs .= '"' . $xmlPath . '" "' . $pdfPath . '"';
	    
        return $this->convert_internal_file_to_file($pathAndArgs, $msgs);
    }


logically, i can usually figure out how to write something in PHP, but actually getting the commands or syntax right is something I sometimes struggle with. anyone who wants to try this out and give me some results, please feel free to do so.[/url]
mikeday
That looks about right, but I think you should add the double quotes around each input path that you add to xmlPath; currently it is still adding double quotes around the whole thing at the end when it gets added to $pathAndArgs. (ie. you want "a" "b" "c", not "a b c").
alyda
ok, finally got the php working to test it out and here it is (it works!)

prince.php:
/////////////////////////////////////////////////////////////////////
	
	 // Convert multiple XML or HTML files to a PDF file.
    // xmlPath: The array of filenames of the 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_files_to_pdf($xmlPaths, $pdfPath, &$msgs = array())
    {
//	set new variable to hold file names as string separated by spaces
		$xmlPath = "";	
//	get length of array	
		$numFiles = count($xmlPaths);
		for ($i=1 ; $i <= $numFiles; $i++ ) {
			$xmlPath .= '"'.array_shift($xmlPaths).'" ';
		}
	
	$pathAndArgs = $this->getCommandLine();
	$pathAndArgs .= $xmlPath . ' "' . $pdfPath . '"';
	//echo $pathAndArgs."<br/>";
	    
        return $this->convert_internal_file_to_file($pathAndArgs, $msgs);
    }
	
/////////////////////////////////////////////////////////////////////


thanks so much!