6

I don't know how to edit an existing pdf file with Laravel.

I have found many plugin for create PDF but no one help me in my problem.

Can anyone know how to do it?

This is what I have tried so far

$pdf->AddPage();
$source = $pdf->setSourceFile(asset("assets/images/coupon/source/coupon.pdf"));
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx, 10, 10, 90);
$pdf->SetXY(40, 0); // Doesn't work
$pdf->Write(0, 'This is just a simple text'); // Doesn't work
$pdf->Output(); // Doesn't work
Salim Djerbouh
  • 10,719
  • 6
  • 29
  • 61
Nolkyz
  • 65
  • 1
  • 1
  • 7
  • `writeHTMLCell` is not a `FPDF` method. Where does this method come from? Furthermore what I saw from your code: You don't want to edit a PDF - you just want to create a new one? You are not referencing an existing PDF file, because you are missing `$pdf->setSourceFile('PATH/TO/SOURCEFILE')` – codedge May 13 '16 at 13:28
  • It's not my totally code I have the SetSourceFile, ImportPage,UseTemplate but SetXY and Write And Output doesn't work ... – Nolkyz May 13 '16 at 13:47
  • Ok it work ! the source wasn't good ! thanks a lot ! – Nolkyz May 13 '16 at 14:01
  • Glad you got it running! – codedge May 13 '16 at 14:02

1 Answers1

9

There is no built-in way for Laravel to edit PDFs. You can of course use external libs, such as FPDF with the FPDI extension to modify PDFs - the result is a new PDF file. There is also a paid lib PDFlib that is capable of editing PDFs.

To install FPDF + FPDI in Laravel you just need to run:

composer require setasign/fpdf && composer require setasign/fpdi

This will install the base FPDF lib + the extension FPDI. Afterwards you can create a new instance and edit your PDF with:

$pdf = new \FPDI();
$pdf->setSourceFile('PATH/TO/SOURCEFILE');

Please make yourself comfortable with the FPDI documentation

Update: With FPDI you cannot directly edit a PDF file. All FPDI does is extracting content from a given PDF file into a reusable format - the result is a complete new PDF. This information can also be found in the FAQ of FPDI.

codedge
  • 4,754
  • 2
  • 22
  • 38
  • Thanks but I already have found a Fpdf lib for Laravel but no FPDI ... How can I use it ? – Nolkyz May 13 '16 at 12:41
  • What Laravel version do you use? How do you use FPDF - can you show some code? – codedge May 13 '16 at 12:47
  • Thanks I use Laravel 5 and I want to open a pdf and add the user name in Just that – Nolkyz May 13 '16 at 12:59
  • I got one error 'Call to undefined method FPDI::writeHTMLCell()' I add the code – Nolkyz May 13 '16 at 13:08
  • 2
    Please update this answer, it is wrong! You cannot edit a PDF with FPDI but you can "only" import single pages of an existing PDF document into a reusable structure with it. This information can also be found in the [FAQ](https://www.setasign.com/support/faq/fpdi/can-i-edit-a-pdf-document-with-fpdi/#question-202) of FPDI. – Jan Slabon May 13 '16 at 15:17
  • 1
    Thanks @Setasign for making this clear. I updated my post. – codedge May 13 '16 at 15:21