Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create new PDF using TCPDF and import existing PDF into it

Tags:

tcpdf

How can I import an existing pdf file into the new-created pdf using TCPDF?

So assume I am currently doing an invoice section. Once the user click print invoice, I will start gather the data and then create a PDF using TCPDF. However, I need to "attach" another existing PDF into it if available. So assume, in 1 invoice file that is generated from TCPDF consists of 5 pages. And then I have to "attach" another existing PDF into this file. So total will be 6 pages or more, depends on the existing PDF file. The existing PDF file is uploaded by the user. So, the existing PDF file will be uploaded first, then will be added into the new generated invoice file.

Is there any way to achieve it?

like image 419
Vinfoster0701 Avatar asked Nov 29 '16 05:11

Vinfoster0701


People also ask

How do I import a PDF?

Choose File > Import > File. Select the PDF file you want to import and click Import. If the PDF file has more than one page, specify the page number in the Select PDF Page dialog box. Use the slider to display a thumbnail image of the page you want, and then click Select.

How do I upload an image to Tcpdf?

$tcpdf = new TCPDF_TCPDF(); $img = file_get_contents(Mage::getBaseDir('media') . '/dhl/logo. jpg'); $PDF_HEADER_LOGO = $tcpdf->Image('@' . $img);//any image file.


1 Answers

You can achieve this with the addition of FPDI:

<?php
require_once('tcpdf.php');
require_once('fpdi.php');

$pdf = new FPDI();

//Merging of the existing PDF pages to the final PDF
$pageCount = $pdf->setSourceFile('existing_pdf.pdf');
for ($i = 1; $i <= $pageCount; $i++) {
    $tplIdx = $pdf->importPage($i, '/MediaBox');
    $pdf->AddPage();
    $pdf->useTemplate($tplIdx);
}

//Your code relative to the invoice here

$pdf->Output();
like image 146
Veve Avatar answered Oct 24 '22 01:10

Veve