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?
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.
$tcpdf = new TCPDF_TCPDF(); $img = file_get_contents(Mage::getBaseDir('media') . '/dhl/logo. jpg'); $PDF_HEADER_LOGO = $tcpdf->Image('@' . $img);//any image file.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With