php FPDF:在单元格内更改文本颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14328848/
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 07:04:55  来源:igfitidea点击:

FPDF: Change text color while inside a Cell?

phptextcolorscellfpdf

提问by Michael Crothers

I want it so that the text saying white will use SetTextColor as white, and the orange to use orange.

我想要它,这样说白色的文字将使用 SetTextColor 作为白色,而橙色使用橙色。

$pdf->SetTextColor(255,255,255);
$pdf->Cell(50,0,'WHITE ORANGE ORANGE WHITE',0,1,'C');

How do I affect the 'ORANGE' words to use an orange text color?

我如何影响“橙色”字样以使用橙色文本颜色?

回答by Ioulian Alexeev

I needed that functionality too. This is the function I written to do a simple colored string:

我也需要那个功能。这是我为做一个简单的彩色字符串而编写的函数:

function cellMultiColor($stringParts) {
    $currentPointerPosition = 0;
    foreach ($stringParts as $part) {
        // Set the pointer to the end of the previous string part
        $this->_pdf->SetX($currentPointerPosition);

        // Get the color from the string part
        $this->_pdf->SetTextColor($part['color'][0], $part['color'][1], $part['color'][2]);

        $this->_pdf->Cell($this->_pdf->GetStringWidth($part['text']), 10, $part['text']);

        // Update the pointer to the end of the current string part
        $currentPointerPosition += $this->_pdf->GetStringWidth($part['text']);
    }

and you use it like this:

你像这样使用它:

cellMultiColor([
    [
        'text' => 'Colored string example: ',
        'color' => [0, 0, 0],
    ],
    [
        'text' => 'red',
        'color' => [255, 0, 0],
    ],
    [
        'text' => ', ',
        'color' => [0, 0, 0],
    ],
    [
        'text' => 'blue',
        'color' => [0, 0, 255],
    ],
]);

回答by Eder Franco

It is possible with a little trick. I just made it printing 2 Cells, one over the other, like this:

这是可能的一个小技巧。我只是让它打印了 2 个单元格,一个接一个,如下所示:

//Setting the text color to black
$pdf->SetTextColor(0,0,0);

//Printing my cell      
$pdf->SetFont('Arial','B');
$pdf->Cell(55,5,"Black Text ",1,0,'C');
$pdf->SetXY($coordXbase,$coordY);

//Setting the text color to red
$pdf->SetTextColor(194,8,8);

//Printing another cell, over the other
$pdf->SetFont('Arial','B');
//Give some space from the left border, and print the red text after the black text that is in the cell behind this one.
$pdf->Cell(55,5,"                        Red Text",0,0,'C');
$pdf->SetXY($coordXbase,$coordY);

//Setting the text color back to back, in the next cells.
$pdf->SetTextColor(0,0,0);

The result was this:

结果是这样的:

Multicollor cell result

多色单元格结果

As I was a little rush, I had no time to create some function to help with this, but this would be a good starting point idea :)

由于我有点匆忙,我没有时间创建一些函数来帮助解决这个问题,但这将是一个很好的起点:)

P.S.: Tell if you guys find an easier way.

PS:告诉你们是否找到更简单的方法。

回答by rod_torres

IF you don't need to use the Cell method, you could use the Write method instead:

如果您不需要使用 Cell 方法,则可以改用 Write 方法:

$pdf->SetFont('Arial','b',12);
$pdf->SetTextColor(153,0,153);
$pdf->Write(7,'Text in color, ');
$pdf->SetFont('Arial','',12);
$pdf->SetTextColor(0,0,0);
$pdf->Write(7,'and text in black all in the same line'));
$pdf->Ln(7);

回答by codermonkey

I had to do a similar thing. Instead of color I had to change the size of the font. in my cell I called the function instead so,in your case you can do this

我不得不做类似的事情。我不得不改变字体的大小,而不是颜色。在我的单元格中,我调用了该函数,因此,在您的情况下,您可以执行此操作

$pdf->Cell(50,0,white($pdf,'White').orange($pdf,'orange'),0,1,'C');

and define the function as

并将函数定义为

function white($pdf,$val){
              $pdf->SetTextColor(255,255,255);
              return $pdf->Text(0,0,$val);
              }

and same goes for orange.

橙色也一样。

TIP: to position it properly use getX() and getY()

提示:正确定位它使用 getX() 和 getY()

回答by Marc Rochkind

Answer #1: You can't. A cell by definition is uniform in font and color. You can measure the width of the words with getStringWidth and do it in a series of Cells.

答案#1:你不能。根据定义,单元格的字体和颜色是统一的。您可以使用 getStringWidth 测量单词的宽度,并在一系列 Cell 中进行。

Answer #2: Many of the contributed scripts are based on constructing variants of built-in functions. After all, you have the PHP code right there for all of FPDF. You can make your own Cell_plus function that takes an array of phrases and another array or two or three of attributes. Then maybe contribute it as an additional script.

答案 #2:许多贡献的脚本都基于构建内置函数的变体。毕竟,您拥有所有 FPDF 的 PHP 代码。您可以创建自己的 Cell_plus 函数,该函数采用短语数组和另一个数组或两个或三个属性。然后也许可以将其作为附加脚本进行贡献。

回答by Maxim Ilin

You may also use a writeHTMLmethod (tcpdf ver 6.2) like so

您也可以使用这样的writeHTML方法(tcpdf ver 6.2)

$html = 'Simple text <span style="color: rgb(255,66,14);">Orange</span> simple  <span style="color: rgb(12,128,128);">Turquoise</span>';
$this->writeHTML($html, true, false, true, false, '');

回答by kelvin2710

You should replace this:

你应该替换这个:

$pdf->Cell(50,0,'WHITE ORANGE ORANGE WHITE',0,1,'C');

for this

为了这

$pdf->Cell(50,0,'WHITE ORANGE ORANGE WHITE',0,1,'C', true);

//Add the true param at the end

//在最后添加真正的参数