简体   繁体   中英

Using FPDI to open a PDF and write text to it

I'm trying to open a PDF and add text to it using this library:

https://www.setasign.com/products/fpdi/downloads/

This is the demo code:

https://www.setasign.com/products/fpdi/demos/simple-demo/#p-283

I've been at this for several days and I'm at my wit's end. It's been a non-stop series of errors about not being able to find this file, or that class not being defined. Is there a simple way to just include one class file and start using it? Using Composer seems to put so many files all over the place that I can hardly understand what I am supposed to do.

Thank you so much in advance.

I will attempt to answer your question here, because I also struggled with the same thing.

So, I started by downloading FPDI, which you've already done. After struggling and figuring out why I couldn't get it to work, I found out I was missing another library. So, go here: FPDF

Once you download that, and include it in the same folder and include it, you should be good to go. For example:

require_once("_include/fpdf/fpdf.php");
require_once("_include/fpdi/src/autoload.php");
use setasign\Fpdi\Fpdi;

Once you have that at the top of your file, you can try something similar:

$newfname = 'tempFile'.date("Y-m-d h:m:s").'.pdf';
   $file = fopen ($url, 'rb');
   if ($file) {
       $newf = fopen ("_data/$newfname", 'wb');
       if ($newf) {
           while(!feof($file)) {
               fwrite($newf, fread($file, 1024 * 8), 1024 * 8);
           }
       }
   }
   if ($file) {
       fclose($file);
   }
   if ($newf) {
       fclose($newf);
   }

   $pdfName = $filename.".pdf";
   $file = fopen("_data/$newfname", 'rb');
   if($file) {
       $newPdfF = fopen("_data/$pdfName", 'wb');
       if($newPdfF) {
           // Now let us use this file to try and remove the bottom logos.
           $pdf = new Fpdi();
           $pageCount = $pdf->setSourceFile($file);
           // Iterate through our pages
           for($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
               // import our page
               $templateId = $pdf->importPage($pageNo);
               // Add our new page
               $pdf->AddPage();

               $pdf->useTemplate($templateId, ['w'=>200, 'h'=>200]);
               $pdf->SetFont('Helvetica');
               $pdf->setX(170);
               $pdf->Write(0, $fileDateRun); // For writing text
               $pdf->Image('images/image.png', 0, 257); // For writing an image
          }
          $pdf->Output('D', "_data/$pdfName");
          unlink("_data/$newfname"); // delete temp file
          unlink("_data/$pdfName"); // delete the other temp file
      }
  }

I hope this helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM