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

fpdf-php-中心单元格上的不同样式

  •  2
  • Emiliano  · 技术社区  · 6 年前

    我的问题是,我在一个单元格中有一个中心文本,我想要用粗体写“client:”这个词,其余的用常规的,就像是居中的,我不能打印“client:”第一个和之后打印名字,也不要用“write”功能,因为是居中的,请帮助。

        $pdf->SetTextColor(102, 106, 117);
        $pdf->SetFont('Arial', 'B', 15);
        $pdf->Cell(626,25,"Client: ".$name,0,0,'C',0);
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Bharata colxi    6 年前

    我们必须计算居中文本的位置,如下所示:

    require("fpdf.php");
    
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetTextColor(102, 106, 117);
    $fullCellWidth = $pdf->GetPageWidth();
    
    $pdf->SetFont("Arial", "", 15);
    $regularCell = "some name";
    $regularWidth = $pdf->GetStringWidth($regularCell);
    
    $pdf->SetFont("Arial", "B", 15);
    $boldCell = "Client: ";
    $boldWidth = $pdf->GetStringWidth($boldCell);
    
    $centerIndentX = ($fullCellWidth - $boldWidth - $regularWidth) / 2;
    
    $pdf->SetX($centerIndentX);
    $pdf->Cell($boldWidth, 25, $boldCell, 0, 0, "L");
    
    $pdf->SetX($centerIndentX + $boldWidth);
    $pdf->SetFont("Arial", "", 15);
    $pdf->Cell($regularWidth, 25, $regularCell, 0, 0, "L");
    
    $pdf->Output();
    

    输出PDF示例-屏幕截图的一部分:

    enter image description here