I am generating a PDF document using TCPDF. My requirement is to merge an existing PDF content at the last page of the dynamically generated PDF.
By far the best solution to your problem is to use FPDI.
https://github.com/Setasign/FPDI
The way it works is that FPDI extends TCPDF so you can work with an FPDI object using all of the methods that you're used to using with TCPDF, but with the additional methods that you need to import pages from existing PDF files (setSourceFile, getTemplateSize and useTemplate).
It looks a bit daunting to set up, but if you're using Composer it is actually incredibly easy. Just add setasign/fpdi and setasign/fpdi-tcpdf to your composer.json file and then use an instance of FPDI in place of your TCPDF instance. I found I didn't even have to call class_exists('TCPDF', true) as mentioned on the github page. Once I added those other entries to composer.json and ran composer dumpautoload it just worked.
This is still in development for TCPDF: http://www.tcpdf.org/doc/code/classTCPDF__IMPORT.html#a5a9effc936e8fa461c0f6717c2d10d93
If possible you can use ZEND:
require_once 'Zend/Pdf.php';
$pdf1 = Zend_Pdf::load("1.pdf");
$pdf2 = Zend_Pdf::load("2.pdf");
foreach ($pdf2->pages as $page){
$pdf1->pages[] = $page;
}
$pdf1->save('3.pdf');
If you are running on Linux, you can also run a shell command.
<?php
exec('pdfjam 1.pdf 2.pdf -o 3.pdf'); // -o = output
You can install pdfjam from here: http://www2.warwick.ac.uk/fac/sci/statistics/staff/academic/firth/software/pdfjam/pdfjam_latest.tgz
I have tried the free version of FPDI but does not support PDF version 1.5 or above.
If someone else is looking for a free solution I have used TCPDI. You can find it on github.
My project does not use composer, so I used the master branch from https://github.com/pauln/tcpdi
If you are using composer, you can find some fork for composer too. Just search tcpdi on github.
Once you add it to your project, the code is quite simple.
This is a snippet from my code. I used it to save a copy of the privacy policy (a static pdf) with the user name and agreement date in footer.
// Create new PDF document
$pdf = new TCPDI(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
...
// Add the pages from the source file.
$pagecount = $pdf->setSourceFile($localPrivacy);
for ($i = 1; $i <= $pagecount; $i++) {
$tplidx = $pdf->importPage($i);
$pdf->AddPage();
$pdf->useTemplate($tplidx);
// Add agreement text in document footer
$pdf->SetXY(15,282);
$pdf->Cell(180, 5, "Documento approvato da {$fullName} il {$date}", 0, 0, 'C');
}
// Send PDF on output
$pdf->Output(FOLDER_PATH . DIRECTORY_SEPARATOR . "{$userId}.pdf", 'F');
Related
I'm creating a web app that allows a canvas form to insert an image from a HTML canvas into a particular position in multiple PDF files. I had this working with python flask as a back-end but the people that I'm making it for only want it in PHP. I have tried using libraries like FPDI but they only work with PDF versions up to 1.4 while the PDF files we are using are version 1.7.
Does anyone know any possible libraries that can help me solve this issue. I would prefer not to convert the PDF files if possible.
Cheers
With TCPDF you can insert images into a PDF (v.1.7) file:
Requirements
composer require tecnickcom/tcpdf
Example
<?php
require_once __DIR__ . '/vendor/autoload.php';
$pdf = new TCPDF();
$pdf->setPDFVersion('1.7');
$pdf->setAutoPageBreak(true);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->AddPage();
// Insert image
$pdf->setJPEGQuality(100);
$pdf->image(__DIR__ . '/example.jpg', 10, 10);
// Close and output PDF document
//$pdf->output('doc.pdf', 'I');
// Save the pdf file
$content = $pdf->output('', 'S');
file_put_contents('example.pdf', $content);
We (Setasign, creator of FPDI) offer a commercial add-on that let you import PDFs which uses a compression technic that was introduced in PDF 1.5.
You may also try to downgrade these documents with an external program. I'm aware of some people using Ghostscript for this.
Generally you should know that you do not insert an image into the existing PDF but you create a completely new PDF while importing a single page into a reusable structure which you place onto a newly created page. On top of this you place the image.
With FPDI you cannot edit a PDF document.
I am trying to import a pdf document with FPDI and add some text to it but when I try to open the new pdf get the error message "format error: not a PDF or corrupted"!
Here is the code:
require_once ('fpdf.php');
require_once ('fpdi.php');
// initiate FPDI
$pdf = new FPDI();
// add a page kl
$pdf->AddPage();
// set the sourcefile
$pdf->setSourceFile('doc1.pdf');
// import page 1
$tplIdx = $pdf->importPage(1);
// use the imported page
$pdf->useTemplate($tplIdx);
// now write some text above the imported page
$pdf->SetFont('Arial');
$pdf->SetTextColor(255,0,0);
$pdf->SetXY(25, 25);
$pdf->Write(0, "This is just a simple text");
$pdf->Output('newpdf.pdf', 'D');
I downloaded this code from the FPDI website and customized this.
The PDF version of my file is 1.3 . (The documentation says FPDI can work with PDFs up to version 1.5)
Tried older versions of FPDI and fpdf_tpl.php but still get this error.
I couldn't find the answer neither with GOOGLE nor in this forum!
Any help appreciated!
UPDATE
In the meantime I found the problem!
All the files were up to date except FPDF! I used an old version of FPDF. Now I use 1.7 instead of 1.5.
from the website, FPDF has to be version 1.6 or below to work with FPDI
I have a doubt about using TCPDF and FPDI together.
I am working on a project where I need to modify existing PDF file and generate new PDF, actually existing PDF are Greeting card template and I have to print certain data at certain pages (such as Image on first page, message on 3rd page, artwork on 4th page) to generate final PDF.
I googled and found with TCPDF, it is not possible to manipulate existing PDF, they suggested using FPDI for opening and manipulating existing PDF.
That's where i am stuck. I need TCPDF (it methods to print images, transparent images, utf text, embed font etc) to do what I want to do, but, I need FPDI to start with. :(.
Please help me: is it possible to use both FPDI and TCPDF together? so that I can use features offered by both APIs ?
Thanks in advance...
http://www.setasign.de/products/pdf-php-solutions/fpdi/about/
"As of version 1.2.1 FPDI can be used with TCPDF - a derivate of FPDF."
libraries_load('tcpdf');
libraries_load('fpdi');
$pdf = new FPDI();
$pdf->setSourceFile("%local_file_path%");
$tplIdx = $pdf->importPage(1);
$pdf->AddPage('L', array(3.5, 2), FALSE);
$pdf->useTemplate($tplIdx, 0, 0, 3.5, 2, false);
I was using Drupal at the time so I used libraries_load but require_once should work.
How can I create a new document using other PDFs that I'm generating?
I have methods to create some documents, and I want to merge them all in a big PDF, how can I do that with TCPDF?
I do not want to use other libs.
TCPDF has a tcpdf_import class, added in 2011, but it is still "under development". If you don't want to use anything outside of TCPDF, you're out of luck!
But FPDI is an excellent addition to TCPDF: it's like an addon. It's as simple as this:
require_once('tcpdf/tcpdf.php');
require_once('fpdi/fpdi.php'); // the addon
// FPDI extends the TCPDF class, so you keep all TCPDF functionality
$pdf = new FPDI();
$pdf->setSourceFile("document.pdf"); // must be pdf version 1.4 or below
// FPDI's importPage returns an object that you can insert with TCPDF's useTemplate
$pdf->useTemplate($pdf->importPage(1));
Done!
See also this question:
TCPDF and FPDI with multiple pages
Why don't you use Zend_PDF, it 's really a very good way to merge file.
<?php
require_once 'Zend/Pdf.php';
$pdf1 = Zend_Pdf::load("1.pdf");
$pdf2 = Zend_Pdf::load("2.pdf");
foreach ($pdf2->pages as $page){
$pdf1->pages[] = $page;
}
$pdf1->save('3.pdf');
?>
Hi i think TCPDF is not able to merge pdf files.
You can try it with an shell command and
PDFTK Toolkit
So you dont have to use an other pdf library.
This thread is from 2009, but using existing PDFs in PHP is still an issue in 2020.
After Zend_PDF has been abandoned and TCPDI does not support PHP 7, FPDI currently seems one of the few working solutions left in 2020. It can be used with TCPDF and FPDF, so existing code keeps working. And it currently seems well maintained.
Check out FPDI and FPDF_TPL. This isn't a perfect solution, but you can basically use FPDF_TPL to create a template of your PDF file and the insert it into your PDF file.
I have several PDF templates that I would like to load and modify and output using tcpdf.
Is it possible to load an existing PDF and use it as a starting point in tcpdf?
You want to use FPDI.
There's some example code here.
I have tried the free version of FPDI but does not support PDF version 1.5 or higher.
If someone else is looking for a free solution I have used TCPDI. You can find it on github https://github.com/pauln/tcpdi
If you are using composer, you can find some fork for composer too. Just search tcpdi on github.
Once you add it to your project, the code is quite simple. It is an extension of TCPDF so all your previous code keep working
This is a snippet from my code. I used it to save a copy of the privacy policy (a static pdf) with the user name and agreement date on each page footer.
// Create new PDF document
$pdf = new TCPDI(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
...
// Add the pages from the source file.
$pagecount = $pdf->setSourceFile($policyPdfPath);
for ($i = 1; $i <= $pagecount; $i++) {
$tplidx = $pdf->importPage($i);
$pdf->AddPage();
$pdf->useTemplate($tplidx);
// Add agreement text in document footer
$pdf->SetXY(15,282);
$pdf->Cell(180, 5, "Documento approvato da {$fullName} il {$date}", 0, 0, 'C');
}
// Send PDF on output
$pdf->Output(FOLDER_PATH . DIRECTORY_SEPARATOR . "{$userId}.pdf", 'F');
For anyone else finding this, it does appear a PARSER and import class were built for TCPDF (https://tcpdf.org/docs/srcdoc/TCPDF/source-class-TCPDF_IMPORT/#50-100) but as of 2018 was still under development.
Its also worth noting that the solutions above do not allow the contents of the PDF pages to be edited. In other words you import the page as a whole, you cannot edit text content or images.
You can use fpdf with fpdi. You can’t modify directly a template, but you can add a text cell with a white background to cache the old content (note that the old content can be read by using some tools). Then save your new pdf. This tools are relatively easy to use. To resolve the fact that fpdi not read 1.5 pdf version in the free version, you can convert 1.5 version in 1.4 version by using ghost script with the exec command. I use this and work fine.