尝试将 PDF 导入 FPDF/FPDI 添加空白页

Trying to Import PDF into FPDF/FPDI Adds Blank Page

我刚开始使用 FPDF / FPDI。我的前任在这里扩展了 FPDF class,我将添加到他的代码中以编辑他的 PDF 并制作新的 PDF。我已经能够通过使用 FPDF 将数据库中的值写入空白页来创建我想要制作的 PDF 的前 3 页。一切顺利!

现在我想将现有的单页 PDF 导入为第 4 页并在上面写一些东西,看起来我需要 FPDI 才能做到这一点。

我在这里使用了示例:https://www.setasign.com/products/fpdi/about 另一个 post 的建议。但是,它只是在我的 PDF 末尾添加了一个空白页。

我已经将要导入到我的日志中的 PDF 的路径写入并确保它是正确的(我在 php 日志中也没有收到任何错误,如果找不到 pdf)。

我正在将 pdf 初始化为 FPDF,而不是 FPDI,所以这可能是问题所在?但是,如果我将它初始化为 FPDI,那么我会收到一个错误消息,指出我的方法未定义,因为它们是在扩展 FPDF class 的基础上定义的。所以我不确定如何做我想做的事...我是否需要重新定义我的 classes 来扩展 FPDI?我只是担心这会破坏已经使用某些相同方法创建的 PDF。我也没有收到有关使用 useImportedPage 等 FPDI 方法的任何错误...所以我觉得这可能不是问题所在?

抱歉,如果我没有很好地解释这一点,如果您有任何问题,请告诉我。相关代码如下:

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);
      }

这行得通!看起来我需要 importPage 的第二个参数来定义边界框

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);
}