代码之家  ›  专栏  ›  技术社区  ›  alien

使用带有多单元的表格在表格行中显示fpdf图像

  •  0
  • alien  · 技术社区  · 8 年前

    我在使用fpdf库显示图像时遇到问题。我将fpdf表与多单元脚本一起使用[ 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_path

    我的pdf将是这样的。 enter image description here

    1 回复  |  直到 8 年前
        1
  •  1
  •   Alien    8 年前

    使用Inhertence修改PDF\u MC\u Table class 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);