FPDF,FPDI - 如何使用PHP将单独的文件与唯一文本组合在一起?

时间:2014-07-31 15:27:37

标签: php fpdf fpdi

我坚持使用fpdf和fpdi。

我使用此代码从5个或更多文件创建1个pdf文件(!important),并希望在每个页面上输入唯一的文本。当前形式的代码将page01test放在每个页面上,而不是第2页上的page02test等。

导入的每个文档都包含一个页面。

我从以下代码获得了此代码的基础:http://www.setasign.com/products/fpdi/demos/concatenate-fake/

require('fpdf.php');
require('fpdi.php');
class ConcatPdf extends FPDI
{
public $files = array();

public function setFiles($files)
{
    $this->files = $files;
}

public function concat()
{
    foreach($this->files AS $file) {
        $pageCount = $this->setSourceFile($file);
        for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
            $tplIdx = $this->ImportPage($pageNo);
            $this->AddPage();
            $this->useTemplate($tplIdx);
            $this->AddFont('NotoSans', '', 'notosans.php');
            $this->SetFont('NotoSans', '', 18);
            $this->SetTextColor(63,76,89);

            if ($pageNo == 1) {
                $this->SetXY(55, 94);
                $this->Write(5, 'page01test');
            }
            if ($pageNo == 2) {
                $this->SetXY(55, 94);
                $this->Write(5, 'page02test');
            }
            if ($pageNo == 3) {
                $this->SetXY(55, 94);
                $this->Write(5, 'page03test');
            }
            if ($pageNo == 4) {
                $this->Write(5, 'page04test');
            }
            if ($pageNo == 5) {
                $this->SetXY(55, 94);
                $this->Write(5, 'page05test');
            }
        }           
    }
 }
}


$pdf = new ConcatPdf();
$pdf->setFiles(array("Proposal1.pdf", "Proposal2.pdf","Proposal3.pdf", "Proposal4.pdf","Proposal5.pdf"));
$pdf->concat();
$pdf->Output();

1 个答案:

答案 0 :(得分:0)

我一直在玩这个,并通过反复试验找到答案,

require('customervalues.php');
require('fpdf.php');
require('fpdi.php');

class ConcatPdf extends FPDI
{
public $files = array();

public function setFiles($files)
{
    $this->files = $files;
}

public function concat()
{
    foreach ($this->files AS $file) {
        $pageCount = $this->setSourceFile($file);
        for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
            $tplIdx = $this->ImportPage($pageNo);
            $this->AddPage();
            $this->useTemplate($tplIdx);
            $this->AddFont('NotoSans', '', 'notosans.php');
            $this->SetFont('NotoSans', '', 18);
            $this->SetTextColor(63,76,89);
            if ($file == 'Proposal1.pdf') {
                $this->SetXY(55, 94);
                $this->Write(5, 'page01test');
            }
            if ($file == 'Proposal2.pdf') {
                $this->SetXY(55, 94);
                $this->Write(5, 'page02test');
            }
            if ($file == 'Proposalxl.pdf') {
                $this->SetXY(55, 94);
                $this->Write(5, 'page03test');
            }
            if ($file == 'Proposal4.pdf') {
                $this->SetXY(55, 94);
                $this->Write(5, 'page04test');
                $this->SetXY(55, 150);
                $this->Write(5, 'page04test2');
            }
            if ($file == 'Proposal5.pdf') {
                $this->SetXY(55, 94);
                $this->Write(5, 'page05test');
            }

        }           
    }
}
}

$pdf = new ConcatPdf();
$pdf->setFiles(array("Proposal1.pdf", "Proposal2.pdf","Proposalxl.pdf", "Proposal4.pdf","Proposal5.pdf"));
$pdf->concat();
$pdf->Output();

这允许我在每个页面上写入唯一的文本并将其导出为最终文档。可能有更便宜的方法,但它可以满足我的需求。