php FPDF - 内联粗体文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22697527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:11:23  来源:igfitidea点击:

FPDF - Inline bold text

phphtmlborderfpdf

提问by I wrestled a bear once.

I am trying to create a PDF from PHP and for legal reason we need to make part of our disclaimer BOLD and the disclaimer needs to be outlined.

我正在尝试从 PHP 创建 PDF,出于法律原因,我们需要将免责声明的一部分设为 BOLD,并且需要概述免责声明。

My current code uses:

我当前的代码使用:

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

I am currently using WriteHTML for other parts of the document, and I could easily use that instead of MultiCell, but then how do I create the border?

我目前正在将 WriteHTML 用于文档的其他部分,我可以轻松地使用它代替 MultiCell,但是如何创建边框?

So I have 2 options..

所以我有2个选择..

native FPDF functions

原生 FPDF 函数

PROS: Give an option for a border

优点:提供边框选项

CONS: No easy way to make inline text bold

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

WriteHTML extension class

WriteHTML 扩展类

PROS: Lets me easily add bold text inline

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

CONS: Not sure how to create the border

缺点:不确定如何创建边框

Suggestions?

建议?

采纳答案by Venom

You could rewrite a part of the extension but it may be easier for you to use the extension writeHTML, then draw a cell (empty text) with a border over the cell you created with writeHTML. By adjusting your cells properly it should work.

您可以重写扩展的一部分,但使用扩展 writeHTML 可能更容易,然后在您使用 writeHTML 创建的单元格上绘制一个带有边框的单元格(空文本)。通过正确调整您的细胞,它应该可以工作。

Don't forget to use SetY thenSetX to position your cells.

不要忘记使用 SetY然后使用SetX 来定位您的单元格。

Example:

例子:

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

回答by Timothy Law

Use Write() & Rect().

使用Write() 和 Rect()

Example:

例子:

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

You need to play around with width & height of the Rect() parameters. In this case, I set width = 2 and height to 0.1 User Units.

您需要调整 Rect() 参数的宽度和高度。在这种情况下,我将 width = 2 和 height 设置为 0.1 User Units

回答by mkrell

Here's how I solved it:

这是我解决它的方法:

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

Essentially, make one cell, change font, then another cell with the width of the text you want to bold, and so on.

本质上,创建一个单元格,更改字体,然后是另一个具有您想要加粗的文本宽度的单元格,依此类推。

There seem to be better tools out there for making PDFs that use HTML / Blade templates and such, though, so you might want to consider using that.

不过,似乎有更好的工具可以制作使用 HTML/Blade 模板等的 PDF,因此您可能需要考虑使用它。

回答by Juan Esteban Berrío

I find other soluction.

我找到其他解决方案。

http://fpdf.de/downloads/add-ons/tag-based-formatting.html

http://fpdf.de/downloads/add-ons/tag-based-formatting.html

copy and paste fragment code write_tag inside file fpdf.php, call function write_tag while was create of pdf, whit this I can make format at the text in pdf.

将片段代码 write_tag 复制并粘贴到文件 fpdf.php 中,在创建 pdf 时调用函数 write_tag,这样我就可以在 pdf 中的文本中进行格式化。

回答by Tu?ba Nur ?ak?

TCPDF more useful than FPDF

TCPDF 比 FPDF 更有用

In TCPDF

在 TCPDF 中

If you set the $ishtmlparameter to truein multicell, you can adapt the html properties to your texts.

如果在多单元格中将$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);

Default function;

默认功能;

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

Function you need to do

你需要做的功能

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

I hope it helps..

我希望它有帮助..

回答by lubosdz

Alternatively TCPDFsupports writing Cell containing HTML via writeHTMLCell(), syntax here.

或者,TCPDF支持通过writeHTMLCell()编写包含 HTML 的 Cell ,这里的语法是。

$pdf->writeHTMLCell($width, $height, $x, $y, "<b>bold</b> and <u>underlined</u> text.");