【问题标题】:How to set 2 different color for different texy in FPDF如何为 FPDF 中的不同文本设置 2 种不同颜色
【发布时间】:2015-06-01 20:29:59
【问题描述】:

如何在 FPDF 中设置 2 种不同的颜色??

我试过下面的代码

$pdf=new FPDF();                     
$pdf->AddPage('P', 'A5');
$pdf-> SetMargins(25, 50);
$pdf->SetTextColor(91,137,42); 
$pdf->SetFont('times','',10);
$pdf -> Text (60,  37,  'Title' ); 
$pdf->SetTextColor(0,0,0);
$pdf->SetFont('times','',15);
$pdf -> Text (60,  37,  'Invoice' );

但对于所有测试,它都显示单一颜色。

有什么解决办法吗?

【问题讨论】:

  • 好吧,你不会用你最初设置的颜色创建任何东西。那你为什么会期望得到两种颜色的东西呢?
  • 这只是exp,我设置了0 0 0,但实际上我需要以红色显示。其他的我需要以绿色显示。
  • 设置第一种颜色后你不会输出任何东西。如果您不输出任何内容,那么您将看不到任何内容。您仅在第二次设置颜色后才输出任何内容。这就是为什么你会以一种颜色看到所有东西。
  • 顺便说一句:您应该改用 fpdf 继任者 tfpdf。相同的代码库,相同的功能集,但启用了 unicode。
  • 我确实检查过,您的代码按预期工作。我在下面的答案中发布了我的支票。

标签: php pdf pdf-generation fpdf


【解决方案1】:

我从你的代码中做了一个最小的工作示例:

<?php
require('fpdf/fpdf.php');    
$pdf=new FPDF(); 
$pdf->AddPage('P', 'A5'); 
$pdf->SetMargins(25, 50); 
$pdf->SetTextColor(91,137,42); 
$pdf->SetFont('times','',10); 
$pdf->Text (60,  27,  'Title' ); 
$pdf->SetTextColor(99,0,0); 
$pdf->SetFont('times','',15); 
$pdf->Text (60,  57,  'Invoice' );    
$pdf->Output('test.pdf');

打开生成的文档 (okular test.pdf),更改颜色值并重新运行代码时,我可以看到文档得到更新并且颜色发生变化。该代码按预期工作。


考虑到您的问题的第一个版本和您的 cmets,我可以想象您的问题是在页面标题中获取文本颜色?如果您遵循文档,这也可以按预期工作:

<?php
require('fpdf/fpdf.php');

class myPDF extends FPDF {
  function Header() {
    $this->SetFont('Arial','B',15);
    $this->setTextColor(0, 120, 120);
    $this->Cell(80);
    $this->Cell(30,10,'Page title',1,0,'C');
    $this->Ln(20);
  }
}

$pdf=new myPDF();
$pdf->AddPage('P', 'A5');
$pdf->SetMargins(25, 50);
$pdf->SetTextColor(91,137,42);
$pdf->SetFont('times','',10);
$pdf->Text (60,  27,  'Heading' );
$pdf->SetTextColor(99,0,0);
$pdf->SetFont('times','',15);
$pdf->Text (60,  57,  'Invoice' );
$pdf->Output('test.pdf');

请注意,在这种情况下,必须在 Header() 函数中设置文本颜色,而不是稍后,当您创建具有标题的页面时...

这是生成的文档,可以清楚地看到颜色:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多