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

Mic*_*ers 7 php text colors cell fpdf

我想要它,以便说白色的文本将使用SetTextColor作为白色,而橙色使用橙色.

$pdf->SetTextColor(255,255,255);
$pdf->Cell(50,0,'WHITE ORANGE ORANGE WHITE',0,1,'C');
Run Code Online (Sandbox Code Playgroud)

如何影响'ORANGE'字样以使用橙色文字颜色?

Ede*_*nco 5

这是可能的一个小技巧。我只是让它打印了 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);
Run Code Online (Sandbox Code Playgroud)

结果是这样的:

多色单元格结果

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

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


Iou*_*eev 5

我也需要这个功能.这是我写的一个简单的彩色字符串函数:

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']);
    }
Run Code Online (Sandbox Code Playgroud)

你像这样使用它:

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],
    ],
]);
Run Code Online (Sandbox Code Playgroud)


Mar*_*ind 0

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

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