FPDF, FPDI -我如何使用PHP结合单独的文件,每个文件都有唯一的文本



我坚持使用fpdf和fpdi。

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

每个导入的文档由一页组成。

我得到了这个代码的基础: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();

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

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();

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

最新更新