Php 使用FPDF在图像上写入文本

Php 使用FPDF在图像上写入文本,php,fpdf,Php,Fpdf,我想生成PDF。 我正在使用FPDF生成pdf文件。 我生成包含内容文本和图像的pdf。 现在我想把这个图像作为背景图像。 因此,情况是文本将覆盖图像。我尝试了许多解决方案,但任何解决方案都对我有效。我用下面的代码生成了PDF $pdf = new FPDF(); $title = 'Without User Details'; $img_title = 'Your Voucher Image is-: '; $php2pdf = ''; $php2pdf .= 'Your Voucher C

我想生成PDF。 我正在使用FPDF生成pdf文件。 我生成包含内容文本和图像的pdf。 现在我想把这个图像作为背景图像。 因此,情况是文本将覆盖图像。我尝试了许多解决方案,但任何解决方案都对我有效。我用下面的代码生成了PDF

$pdf = new FPDF();
$title = 'Without User Details';
$img_title = 'Your Voucher Image is-:  ';
$php2pdf = '';
$php2pdf .= 'Your Voucher Code is-:  ';
$php2pdf .= $voucher_prefix;
$php2pdf .= get_post_meta($post->ID, 'voucher_start_digit', true);

 if (has_post_thumbnail( $post->ID ) ):
 $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
 $image_url = $image[0];
 endif;

$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->SetTitle($title);
$pdf->MultiCell($w, $h, $title);
$pdf->Ln(3);
$pdf->Cell(90,90,$php2pdf);
$pdf->Ln(20);
$pdf->MultiCell(40, 10, $img_title);
$pdf->Image($image_url,30,60,90,0); ob_start();
$pdf->Output();
ob_end_flush();

我得到了答案!!!这是通过使用FPDF的透明度实现的。 为此,我们必须使用其扩展的Alphapdf类

参考:

我不知道是否还有人在寻找这个,但在我的例子中,我使用了一个自定义函数,该函数由创建并发布在上,使用时与“BreakCell”下面的单元格重叠,因此您需要设置x和y,但可以在不使用透明度的情况下在图像上写入文本

function BreakCell($w, $h, $txt, $border=0, $align='J', $fill=false){
    if(!isset($this->CurrentFont))
        $this->Error('No font has been set');
    $cw = &$this->CurrentFont['cw'];
    if($w==0)
        $w = $this->w-$this->rMargin-$this->x;
    $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
    $s = str_replace("\r",'',$txt);
    $nb = strlen($s);
    if($nb>0 && $s[$nb-1]=="\n")
        $nb--;
    $b = 0;
    if($border)$b=$border;
    $sep = -1;
    $i = 0;
    $j = 0;
    $l = 0;
    $ns = 0;
    $nl = 1;
    $stop=false;
    while($i<$nb && $stop===false)
    {
        $c = $s[$i];
        if($c=="\n")
        {
            if($this->ws>0)
            {
                $this->ws = 0;
                $this->_out('0 Tw');
            }
            $this->Cell($w,$h,substr($s,$j,$i-$j), 0, 0,'C');
            $stop=true;
            break;
        }
        $l += $cw[$c];
        if($l>$wmax)
        {
            if($sep==-1)
            {
                if($i==$j)
                    $i++;
                if($this->ws>0)
                {
                    $this->ws = 0;
                    $this->_out('0 Tw');
                }
            $this->Cell($w,$h,substr($s,$j,$i-$j-3).'...',0,0,'C');
            $stop=true;
            break;
            }
        }
        else
            $i++;
    }
    if($stop===false){
        if($this->ws>0)
        {
            $this->ws = 0;
            $this->_out('0 Tw');
        }
        $this->Cell($w,$h,substr($s,$j,$i-$j),$b,0,$align,$fill);
    }
    $this->x = $this->lMargin;
}
函数BreakCell($w,$h,$txt,$border=0,$align='J',$fill=false){
如果(!isset($this->CurrentFont))
$this->Error('未设置字体');
$cw=&$this->CurrentFont['cw'];
如果($w==0)
$w=$this->w-$this->rMargin-$this->x;
$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
$s=str_replace(“\r”,”$txt);
$nb=斯特伦($s);
如果($nb>0&&$s[$nb-1]==“\n”)
$nb--;
$b=0;
如果($border)$b=$border;
$sep=-1;
$i=0;
$j=0;
$l=0;
$ns=0;
$nl=1;
$stop=false;
而($iws>0)
{
$this->ws=0;
$this->_out('0 Tw');
}
$this->Cell($w,$h,substr($s,$j,$i-$j),0,0,'C');
$stop=true;
打破
}
$l+=$cw[$c];
如果($l>$wmax)
{
如果($sep==-1)
{
如果($i=$j)
$i++;
如果($this->ws>0)
{
$this->ws=0;
$this->_out('0 Tw');
}
$this->Cell($w,$h,substr($s,$j,$i-$j-3)。“…”,0,0,'C”);
$stop=true;
打破
}
}
其他的
$i++;
}
如果($stop==false){
如果($this->ws>0)
{
$this->ws=0;
$this->_out('0 Tw');
}
$this->Cell($w,$h,substr($s,$j,$i-$j),$b,0,$align,$fill);
}
$this->x=$this->lMargin;
}

试着用这种方法,它会工作的

[![require_once('fpdf/fpdf.php');  
$pdf = new FPDF(); 


$pdf->AddPage();
$pdf->Image('PC_Letterhead.jpg',0,0,210,0); ob_start();
$pdf->SetFont('Arial','B',22);
 $pdf->Cell(0,20,'Your Shipping',0,1); 
$pdf->SetFont('Arial','',12); 

          $pdf->SetFillColor(224,224,224); //Set background of the cell to be that grey color
           $pdf->Cell(40,12,"Label",1,0,'C',true);
          $pdf->Cell(40,12,"Quanitity",1,1,'C',true); //the 1 before the 'C' instead of 0 in previous lines tells it to move down by the height of the cell after writing this
           $pdf->Cell(40,12,"SIZE",1,0,'C');
          $pdf->Cell(40,12,"1",1,1,'C');
            $pdf->Cell(40,12,"COLOR",1,0,'C');
          $pdf->Cell(40,12,"2",1,1,'C');
            $pdf->Cell(40,12,"NECK",1,0,'C');
          $pdf->Cell(40,12,"3",1,1,'C');
            $pdf->Cell(40,12,"HANDLE",1,0,'C');
          $pdf->Cell(40,12,"4",1,1,'C');
            $pdf->Cell(40,12,"VENTS",1,0,'C');
          $pdf->Cell(40,12,"5",1,1,'C');
$pdf->Output();
ob_end_flush();][1]][1]

那么你想把这个图像作为背景图像吗?我已经尝试过这个解决方案,但是没有成功。它不是将图像作为背景