I w*_*ce. 9 html php border fpdf
我正在尝试使用PHP创建PDF,出于法律原因,我们需要将我们的免责声明作为BOLD的一部分,并且需要概述免责声明.
我目前的代码使用:
if(isset($_POST['optout']) && $_POST['optout'] == "yes"){
$pdf->Ln(5);
$pdf->SetFont('Arial','I',12);
$pdf->SetTextColor(128);
$pdf->MultiCell(0,4,'This is my disclaimer. THESE WORDS NEED TO BE BOLD. These words do not need to be bold.',1,'C');
}
Run Code Online (Sandbox Code Playgroud)
我目前正在将WriteHTML用于文档的其他部分,我可以轻松地使用它而不是MultiCell,但是我如何创建边框?
所以我有两个选择..
原生FPDF功能
PROS:为边框提供选项
缺点:没有简单的方法可以使内联文本变粗
WriteHTML扩展类
PROS:让我轻松添加内联粗体文本
缺点:不确定如何创建边框
建议?
小智 9
您可以重写扩展的一部分,但是您可能更容易使用扩展writeHTML,然后在使用writeHTML创建的单元格上绘制带边框的单元格(空文本).通过适当调整细胞,它应该工作.
不要忘记使用SetY 然后使用SetX来定位细胞.
例:
if(isset($_POST['optout']) && $_POST['optout'] == "yes"){
$pdf->Ln(5);
$pdf->SetFont('Arial','I',12);
$pdf->SetTextColor(128);
//Your text cell
$pdf->SetY($pos_Y);
$pdf->SetX($pos_X);
$pdf->writeHTML('This is my disclaimer. <b>THESE WORDS NEED TO BE BOLD.</b> These words do not need to be bold.');
//Your bordered cell
$pdf->SetY($pos_Y);
$pdf->SetX($pos_X);
$pdf->Cell($width, $height, '', 1, 0, 'C');
}
Run Code Online (Sandbox Code Playgroud)
我是这样解决的:
$pdf->SetFont('Arial','',10);
$cell = 'This is my disclaimer.';
$pdf->Cell($pdf->GetStringWidth($cell),3,$cell, 0, 'L');
$pdf->SetFont('Arial','B',10);
$boldCell = "THESE WORDS NEED TO BE BOLD.";
$pdf->Cell($pdf->GetStringWidth($boldCell),3,$boldCell, 0, 'L');
$pdf->SetFont('Arial','',10);
$cell = 'These words do not need to be bold.';
$pdf->Cell($pdf->GetStringWidth($cell),3,$cell, 0, 'L');
Run Code Online (Sandbox Code Playgroud)
本质上,创建一个单元格,更改字体,然后使用您想要加粗的文本宽度创建另一个单元格,依此类推。
不过,似乎有更好的工具可以使用 HTML / Blade 模板等来制作 PDF,因此您可能需要考虑使用它。
例:
$pdf->Rect($pdf->GetX(),$pdf->GetY(),2,0.1);
$pdf->SetFont('Arial','',8);
$pdf->Write(0.1,"this is not bold, but this ");
$pdf->SetFont('Arial','B',8);
$pdf->Write(0.1,"is bold.");
$pdf->SetFont('Arial','',8);
$pdf->Ln();
Run Code Online (Sandbox Code Playgroud)
您需要调整Rect()参数的宽度和高度。在这种情况下,我将width = 2和height设置为0.1 User Units。