David
David

Reputation: 728

command line combine pdfs through php exec

I just setup wkhtmltopdf to take html docs and turn them into pdfs but now I need a way to combine the multiple pdfs.

Any suggestions?

We do have pdflib license if there is something it can do but seems its mainly an api for custom building pdfs.

Upvotes: 0

Views: 1130

Answers (1)

Fabio Mora
Fabio Mora

Reputation: 5499

If you got wkhtmltopdf I suppose you're using Linux and you got a root shell access.

You could use Pdftk Toolkit via system() or exec().

First install it. It's a package in Debian/Ubuntu, for example:

sudo apt-get install pdftk

Then make sure that's visible in your bin search PATH.

And a simple PHP snippet:

// In / Out filenames
$input_files = array( 'file_one.pdf', 'file_two.pdf' );
$file_out = 'merged.pdf';

// Escape names and create argument
$line_parts = '';
foreach( $input_files as $file ) {
     $line_parts .= escapeshellarg( $file ) . ' ';
}

// Run pdftk
$cmd = 'pdftk ' . $line_parts . ' cat output ' . escapeshellarg( $file_out );
system( $cmd, $retval );
if( $retval ) { print 'There was an error!'; }

Upvotes: 1

Related Questions