Merge PDF files with PHP [closed]
Below is the php PDF merge command.
$fileArray= array("name1.pdf","name2.pdf","name3.pdf","name4.pdf");$datadir = "save_path/";$outputName = $datadir."merged.pdf";$cmd = "gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$outputName ";//Add each pdf file to the end of the commandforeach($fileArray as $file) { $cmd .= $file." ";}$result = shell_exec($cmd);
I forgot the link from where I found it, but it works fine.
Note: You should have gs (on linux and probably Mac), or Ghostscript (on windows) installed for this to work.
i suggest PDFMerger from github.com, so easy like ::
include 'PDFMerger.php';$pdf = new PDFMerger;$pdf->addPDF('samplepdfs/one.pdf', '1, 3, 4') ->addPDF('samplepdfs/two.pdf', '1-2') ->addPDF('samplepdfs/three.pdf', 'all') ->merge('file', 'samplepdfs/TEST2.pdf'); // REPLACE 'file' WITH 'browser', 'download', 'string', or 'file' for output options
I've done this before. I had a pdf that I generated with fpdf, and I needed to add on a variable amount of PDFs to it.
So I already had an fpdf object and page set up (http://www.fpdf.org/)And I used fpdi to import the files (http://www.setasign.de/products/pdf-php-solutions/fpdi/)FDPI is added by extending the PDF class:
class PDF extends FPDI{} $pdffile = "Filename.pdf"; $pagecount = $pdf->setSourceFile($pdffile); for($i=0; $i<$pagecount; $i++){ $pdf->AddPage(); $tplidx = $pdf->importPage($i+1, '/MediaBox'); $pdf->useTemplate($tplidx, 10, 10, 200); }
This basically makes each pdf into an image to put into your other pdf. It worked amazingly well for what I needed it for.