0

我坚持使用 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();
4

1 回答 1

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

这使我可以在每一页上编写独特的文本并将其导出为最终文档。可能有更便宜的方法可以做到这一点,但它可以满足我的需要。

于 2014-08-01T12:08:48.300 回答