Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php FPDF-内联粗体文本_Php_Html_Border_Fpdf - Fatal编程技术网

Php FPDF-内联粗体文本

Php FPDF-内联粗体文本,php,html,border,fpdf,Php,Html,Border,Fpdf,我正在尝试从PHP创建一个PDF,出于法律原因,我们需要将免责声明的一部分加粗,并且需要对免责声明进行概述 我当前的代码使用: if(isset($_POST['optout']) && $_POST['optout'] == "yes"){ $pdf->Ln(5); $pdf->SetFont('Arial','I',12); $pdf->SetTextColor(128); $pdf->MultiCell(0,4,'Th

我正在尝试从PHP创建一个PDF,出于法律原因,我们需要将免责声明的一部分加粗,并且需要对免责声明进行概述

我当前的代码使用:

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');
}
我目前正在为文档的其他部分使用WriteHTML,我可以很容易地使用它而不是MultiCell,但是如何创建边框呢

所以我有两个选择

本机FPDF功能

优点:为边界提供一个选项

缺点:没有简单的方法使内联文本加粗

WriteHTML扩展类

优点:让我可以轻松地添加粗体文本内联

缺点:不知道如何创建边界


建议?

您可以重写扩展的一部分,但使用扩展名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');
 } 
$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();
if(isset($\u POST['optout'])和&$\u POST['optout']=“是”){
$pdf->Ln(5);
$pdf->SetFont('Arial','I',12);
$pdf->SetTextColor(128);
//你的文本单元格
$pdf->SetY($pos_Y);
$pdf->SetX($pos_X);
$pdf->writeHTML('这是我的免责声明。这些词需要加粗。这些词不需要加粗');
//你的边界细胞
$pdf->SetY($pos_Y);
$pdf->SetX($pos_X);
$pdf->Cell($width,$height,'1,0,'C');
} 
使用

例如:

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');
 } 
$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();

您需要处理Rect()参数的宽度和高度。在本例中,我将width=2,height设置为0.1。

下面是我如何求解它的:

$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');
从本质上讲,创建一个单元格,更改字体,然后创建另一个单元格,将文本的宽度设置为粗体,依此类推

P.F.似乎有更好的工具来制作使用HTML /刀片模板的PDF文件,但是,因此,您可能需要考虑使用它。p> 或者支持通过语法写入包含HTML的单元格

$pdf->writeHTMLCell($width、$height、$x、$y,“粗体加下划线文本”);

我找到了其他解决方案


在文件fpdf.php中复制并粘贴片段代码write_标记,在创建pdf时调用函数write_标记,这样我可以在pdf中的文本中设置格式。

TCPDF比fpdf更有用

以TCPDF格式

如果在multicell中将$ishtml参数设置为true,则可以根据文本调整html属性

$text = "It <b>must</b> be like that";

$this->MultiCell($w, $h, $text, $border, $align, $fill, $ln, $x, $y, $reseth, $stretch, true, $autopadding, $maxh);
你需要做的功能

MultiCell($w, $h, $txt, $border=0, $align='J', $fill=0, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=true, $autopadding=true, $maxh=0)

我希望它有帮助。

这就是我最后要做的,除了我使用了
Rect()
而不是cell。谢谢。当我写“

Word 1:Word 2

”时,它在Word 1和Word 2之间创建了一个换行符,如何解决这个问题?多单元呢?回答一个超过五年的问题。。。大胆的动作。有点像和死熊摔跤:-为什么要像初学者一样投入这么多精力?您可以使用FPDF的核心功能优化工作。