FPDF - Inline bold text

丶灬走出姿态 提交于 2019-12-08 15:57:55

问题


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.

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?

So I have 2 options..

native FPDF functions

PROS: Give an option for a border

CONS: No easy way to make inline text bold

WriteHTML extension class

PROS: Lets me easily add bold text inline

CONS: Not sure how to create the border

Suggestions?


回答1:


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.

Don't forget to use SetY then SetX to position your cells.

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



回答2:


Use 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.




回答3:


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.




回答4:


I find other soluction.

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.




回答5:


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

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



回答6:


TCPDF more useful than FPDF

In TCPDF

If you set the $ishtml parameter to true in multicell, you can adapt the html properties to your texts.

$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..



来源:https://stackoverflow.com/questions/22697527/fpdf-inline-bold-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!