【问题标题】:fpdf display image in table row using table with multilcellfpdf使用带有multilcell的表格在表格行中显示图像
【发布时间】:2017-09-30 07:01:23
【问题描述】:

我在使用 fpdf 库显示图像时遇到问题。我将 fpdf Table 与 MultiCells 脚本一起使用 [http://www.fpdf.org/?go=script&id=3]。

我可以通过调用这个函数来显示数据

$data = [
  ['A','B','C','image_path'],
  ['A','B','C','image_path'],
  ['A','B','C','image_path'],
];
$pdf->Row($header);
foreach($data as $v) $pdf->Row($v);

它会很好地生成数据。我想用 image 替换 image_path 。如何使用此脚本显示图像?

我的 pdf 将是这样的。

【问题讨论】:

    标签: php fpdf


    【解决方案1】:

    使用 Inhertence 修改 PDF_MC_Table 类 Row() 函数。

    Class Pdf extends PDF_MC_Table{
      protected $imageKey = '';
    
      public function setImageKey($key){
        $this->imageKey = $key;
      }
    
      public function Row($data){
        $nb=0;
        for($i=0;$i<count($data);$i++)
          $nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
          $h=5*$nb;
          $this->CheckPageBreak($h);
          for($i=0;$i<count($data);$i++){
            $w=$this->widths[$i];
            $a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
            $x=$this->GetX();
            $y=$this->GetY();
            $this->Rect($x,$y,$w,$h);
    
            //modify functions for image 
            if(!empty($this->imageKey) && in_array($i,$this->imageKey)){
              $ih = $h - 0.5;
              $iw = $w - 0.5;
              $ix = $x + 0.25;
              $iy = $y + 0.25;
              $this->MultiCell($w,5,$this->Image ($data[$i],$ix,$iy,$iw,$ih),0,$a);
            }
            else
              $this->MultiCell($w,5,$data[$i],0,$a);
            $this->SetXY($x+$w,$y);
          }
          $this->Ln($h);
        }
      }
    
    }
    

    现在像这样调用这个函数

    $data = [
      ['A','B','C','image_path'],
      ['A','B','C','image_path'],
      ['A','B','C','image_path'],
    ];
    
    $pdf = new Pdf();
    $pdf->AddPage();
    $pdf->setImageKey = [4];
    $pdf->Row($data);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      • 1970-01-01
      • 2015-07-14
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多