2

I would like to merge multiple PDF files to one. The PDF files are on different sites so they should first be downloaded and merged to one.

We work with PHP and Codeigniter. Does somebody know an solution for my problem?

rcs
  • 67,191
  • 22
  • 172
  • 153
JelleP
  • 976
  • 7
  • 20
  • 39

2 Answers2

6

yes. i have merged pdf's. Get the fpdf and fpdi libraries and put them in to the code igniter third party folder. then it is just a matter of reading the manual for fpdfi and merging the documents. use this to set up the libraries

 require_once("application/third_party/fpdf/fpdf.php");//http://www.fpdf.org/
 require_once("application/third_party/fpdi/FPDI_Protection.php");//http://www.setasign.de/products/pdf-php-solutions/fpdi/

then this snippet of code should give you an idea of how it works. note i have edited sections of this for clarity. this example streams the pdf file. you could just as easily save the file elsewhere.

    $files = array(<file full paths here>);

$pdf = new FPDI_Protection();
if ($data->password)
{
    $pdf->setProtection(array(),$data->password);
}
for ($i = 0; $i < count($files); $i++ )
{
    $pagecount = $pdf->setSourceFile($files[$i]);
    for($j = 0; $j < $pagecount ; $j++)
    {
        $tplidx = $pdf->importPage(($j +1), '/MediaBox'); // template index.
        $pdf->addPage('L','A4');// orientation can be P|L
        $pdf->useTemplate($tplidx, 0, 0, 0, 0, TRUE);                   
    }
    unlink($files[$i]); // you may not want to unlink the files!
}

$dt = new DateTime(NULL, new DateTimeZone($data->user->timezone));
    // set the metadata.
$pdf->SetAuthor($data->user->user_name);
$pdf->SetCreator('website name!');
$pdf->SetTitle('PDF, created: '.$dt->format(MYHMRS_DATETIME_FRIENDLY));
$pdf->SetSubject('PDF subject !');
$pdf->SetKeywords('website name!'.", keywords! ".$data->user->user_name);
$output = $pdf->Output('', 'S');
$name = "PDF".'-'.$dt->format('ymd').'.pdf';

$this->output
    ->set_header("Content-Disposition: filename=$name;")
    ->set_content_type('Application/pdf')
    ->set_output($output);

as for getting pdfs from other sites, firstly make sure you are allowed to do this, then it is matter of using cURL. (copy URL). there is a codeigniter library to do this, or you can use the PHP library.

pgee70
  • 3,707
  • 4
  • 35
  • 41
  • Thanks you for this comment. I tried this in my application. There is one problem left. I cannot find the solution for it. Is gives the error: FPDF error: File is encrypted!. There is no password on the file. This error also occured at the PDF Merge solution but that uses also the FDFI library. – JelleP Nov 08 '13 at 13:04
  • Hmm this solution is oké. It it something with my PDF files. – JelleP Nov 08 '13 at 13:37
2

Reference Link :http://portfolio.floatbit.com/blog/merge-multiple-pdfs-php-using-shellexec

enter image description here

PDF Merge is also a solution

Reference Link :http://pdfmerger.codeplex.com/releases/view/37934

enter image description here

Siraj Khan
  • 2,328
  • 17
  • 18
  • Thanks for your comment. I already tried this solution. The PDF Merge solution gives an error: FPDF error: File is encrypted!. I cannot found the solution for this problem. There is no password on this files. – JelleP Nov 08 '13 at 13:02