Merge Pdf Files With PHP

Merge PDF files with PHP

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.

PHP: Merge PDFs without composer?

You don't need composer to use PDFMerger.

Just clone the original repository from https://github.com/myokyawhtun/PDFMerger and move PDFMerge.php and the tcpdf directory into your project.

Then you can do the following:

include 'PDFMerger.php';

$pdf = new \PDFMerger\PDFMerger;

Merge two PDFs using php

I used pdftk and resolved my problem.

How to merge multiple PDF files to one one-page PDF in PHP

You can do this with FPDF and FPDI.
A good demo to start is available here. You just have to remove the drawing of borders and offsets.

Merge pdf files in PHP and keep weblinks inside

All the mentioned libraries use FPDI under the hood, which simply does not support content outside of a pages content stream, such as links or any other annotation type.

We (author of FPDI) also offer non-free products which work on another level and which allow you keep all annotations including links and also forms when you concatenate the documents. This is possible with the SetaPDF-Merger component:

$merger = new SetaPDF_Merger();

foreach($sourcePdfs as $file) {
$merger->addFile($pdfDir . '/source/' . $file);
}

$merger->merge();

$document = $merger->getDocument();
$document->setWriter(new SetaPDF_Core_Writer_Http('Download.pdf'));
$document->save()->finish();

Merge multiple PDF files into one in PHP

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.

How can I merge more than one PDF in single PDF in PHP

Yes of course you can using your existing FPDF library.
See https://github.com/clegginabox/pdf-merger
Simple and awesome.

Merge two PDF files into single one using MPDF

mPDF is not the best tool to merge PDF files. You'll be better off with GhostScript:

gs -dBATCH -dSAFER -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=combined.pdf invoice1.pdf invoice2.pdf

Alternatively, generate both invoices directly to one file:

$invoice_nos = ['0' => 'ISE-00000014Y18', '1' => 'ISE-00000005Y18'];
$mpdf = new \mPDF('utf-8', 'A4', '', '', 5, 5, 36, 10, 5, 4);

foreach ($invoice_nos as $key => $invoice_no) {
$html = 'Invoice No - ' . $invoice_no;
$mpdf->WriteHTML($html, 2);
$mpdf->WriteHTML('<pagebreak>');
}

$pdf_file_name = $invoice_no . 'invoices.pdf';
$mpdf->Output($pdf_file_name, 'f');


Related Topics



Leave a reply



Submit