tcpdf - start with existing PDF document
Asked Answered
B

4

30

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?

Bunkum answered 13/9, 2009 at 21:56 Comment(0)
C
32

You want to use FPDI.

There's some example code here.

Confucianism answered 13/9, 2009 at 22:0 Comment(7)
@neoneye do you have a link to information about that? My quick look doesn't turn anything up.Confucianism
the problem with fpdi is that the you have to pay if you want to parse pdfs > 1.4Religionism
Be aware that FPDI removes all links and javascript from a PDF.Squatter
@Bunkum : after parsing , are you able to regenerate the pdf?Lorielorien
@KaushaThakkar: I don't remember anymore, this was a problem 8 years ago.Bunkum
FPDI natively supports only pdf to version 1.4. If your pdf is above 1.4 you have to purchase a parser licenceDiarmit
@Sojtin - because it was the best answer 10 years ago. If you know better in 2019, why not write an answer of your own?Confucianism
T
4

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');
Transfer answered 16/4, 2020 at 19:26 Comment(2)
I tried to download TCPDF, FPDI and TCPDI and including them together to do the same, but there was an issue of different versions support between them .. so can you please pack them all into a single archived file and provide it as a shared Google Drive link?Palmitate
@RyadPasha Here the version of the libraries I am using. Hope could be useful. tcpdf version 5.9.149 - tcpdi version 1.1 (based on fpdi version 1.4.4) - tcpdi_parser version 1.1 (based on tcpdf_parser version 1.0.003)Transfer
C
0

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.

Catastrophe answered 16/9, 2020 at 7:36 Comment(0)
C
0

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.

Cardioid answered 22/9, 2021 at 14:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.