Home > Software design >  Trying to Import PDF into FPDF/FPDI Adds Blank Page
Trying to Import PDF into FPDF/FPDI Adds Blank Page

Time:04-19

I'm new to using FPDF / FPDI. My predecessor here extended the FPDF class and I'm adding onto his code to edit his PDFs and make new ones. I've been able to create the first 3 pages of a PDF I want to make by writing values from my database into blank pages, using FPDF. All good!

Now I want to import an existing single page PDF as page 4 and write some stuff on it, and it looks like I need FPDI to do that.

I used the example here: https://www.setasign.com/products/fpdi/about as suggested by another post. However, it's just adding a blank page to the end of my PDF.

I've written the path of the PDF I want to import into my log and ensured that it is correct (I'm not getting any errors in the php log either, which I would expect if the pdf was not found).

I'm initializing the pdf as FPDF, not FPDI, so that may be the issue? But if I initialize it as FPDI then I get an error that my methods are undefined, because they are defined extending the FPDF class. So I'm not sure how to do what I want...do I need to redefine my classes to extend FPDI? I'm just worried this will break the PDFs already being created using some of the same methods. I'm also not getting any errors about using FPDI methods like useImportedPage...so I feel like maybe that's not the issue?

Sorry if I'm not explaining this well, let me know if you have questions. Here is the relevant code:

require_once(APPPATH.'libraries/fpdf/fpdf.php');    
require_once(APPPATH.'libraries/fpdi/autoload.php');
require_once(APPPATH.'libraries/fpdi/Fpdi.php');

public function make_fieldpacketMA(){
        $plotID=$this->input->get('PlotID');
        $year=$this->input->get('Year');

        $pdf = new PDF('P','in','Letter');

        // Make additional info first page- this works!
        $additional_info=$this->fhm_model->get_additionalplotinfo($plotID, "MA");  
        $pdf->AdditionalInfoSheet($additional_info, $pdf);

        //make plot info pages (same as subplot info pages on VT style plots)- this works!
        $plotInfo=$this->fhm_model->get_sheetinfoMA($plotID,$year);
        $pdf->SubplotSheet($plotInfo, "MA", $pdf);
        
        //make seedling sheet from existing template- this does not work
        $seedlingInfo=$this->fhm_model->get_seedlingInfo($plotID, $year);
        $pdf->SeedlingSheet($seedlingInfo, $pdf);
        
        //Write the output
        $rand=uniqid();
        $filename='./fhm_sheets/PlotPacket_'.$rand.'.pdf';

        $pdf->Output($filename,"F");
        $this->load->helper('download');
        
        $data = file_get_contents($filename);
        force_download($plotID.'_'.($year 1).'_FHMPlotPacket.pdf',$data); 
    }

//Create PDF creation class
class PDF extends PDF_Rotate{
            
      function SeedlingSheet($seedlingInfo=array()) {
          $pageCount = $this->setSourceFile(FCPATH.'fhm_template/MAFHM_Microplot_SeedlingForm.pdf');
          $pageId = $this->importPage(1);
          $this->AddPage();
       // $this->useTemplate($pageId);
          $this->useImportedPage($pageId, 10, 10, 90);
      }

CodePudding user response:

This works! Looks like I needed a second argument for importPage to define the bounding box

function SeedlingSheet($seedlingInfo=array()) {
    $pageCount = $this->setSourceFile(FCPATH.'fhm_template/MAFHM_Microplot_SeedlingForm.pdf');
    $pageId = $this->importPage(1, \setasign\Fpdi\PdfReader\PageBoundaries::MEDIA_BOX);
    $this->AddPage();
    $this->useTemplate($pageId, 0, 0);
}
  • Related