Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge PDF files with PHP [closed]

Tags:

php

pdf

My concept is - there are 10 pdf files in a website. User can select some pdf files and then select merge to create a single pdf file which contains the selected pages. How can i do this with php?

like image 442
Imrul.H Avatar asked Jan 25 '11 14:01

Imrul.H


People also ask

Why won't my PDF files merge?

If you don't enter a password when prompted, you won't be able to complete the PDF merging process and you will get an error message. In this case, if you don't have the password, even Adobe Acrobat won't work to combine files. This is where third-party software to combine multiple PDF documents comes into play.

Is PDF Mergy safe to use?

About PDF MergyIt is secure, the files that are uploaded and the merged files will be permanently deleted within a few minutes. It is compatible with ChromeOS, Linux, Mac and Windows.

How do I automatically merge PDF files?

Merge All PDF Files In A Folder Using A Desktop FlowOpen Power Automate Desktop and create a new flow called Merge PDF Document. We want the user to manually select the folder where there are PDFs waiting to be merged. Add a Display select folder dialog action with the title “Select Folder With PDFs To Merge.”


2 Answers

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 command foreach($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.

like image 88
Sanjeev Chauhan Avatar answered Sep 19 '22 12:09

Sanjeev Chauhan


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 
like image 42
AgelessEssence Avatar answered Sep 19 '22 12:09

AgelessEssence