0
我需要爲目錄中的所有pdf文件添加一個遞增的數字。 我可以遍歷所有文件。 我可以在一個文件中寫一個數字,但是當循環開始爲下一個文件,我得到一個錯誤:不能重新聲明類PDF循環瀏覽目錄並在pdf文件中添加一個數字
#!/usr/bin/env php
<?php
$PDFDIR="/home/_Facturen_PDF/";
$FTDILIB="/root/scripts/PDF/resources/FPDI";
require_once($FTDILIB.'/fpdf.php');
require_once($FTDILIB.'/fpdi.php');
if ($handle = opendir($PDFDIR)) {
$blacklist = array('.', '..','._','Q1','Q2','Q3','Q4');
while (false !== ($PDFFILE = readdir($handle))) {
if (!in_array($PDFFILE, $blacklist)) {
//echo $PDFFILE.PHP_EOL;
$fullPathToFile = $PDFDIR.'/'.$PDFFILE;
class PDF extends FPDI {
var $_tplIdx;
function Header() {
global $fullPathToFile;
if (is_null($this->_tplIdx)) {
// THIS IS WHERE YOU GET THE NUMBER OF PAGES
$this->numPages = $this->setSourceFile($fullPathToFile);
$this->_tplIdx = $this->importPage(1);
}
$this->useTemplate($this->_tplIdx, 0, 0,200);
}
function Footer() {}
}
// initiate PDF
$pdf = new PDF();
// add a page
$pdf->AddPage();
// The new content
$pdf->SetFont("helvetica", "B", 25);
$pdf->SetTextColor(255, 0, 0);
$pdf->Text(190,10,"1");
// THIS PUTS THE REMAINDER OF THE PAGES IN
if($pdf->numPages>1) {
for($i=2;$i<=$pdf->numPages;$i++) {
//$pdf->endPage();
$pdf->_tplIdx = $pdf->importPage($i);
$pdf->AddPage();
}
}
//show the PDF in page
$pdf->Output($PDFDIR.'/Q1/'.$PDFFILE, 'F');
}
}
closedir($handle);
}
?>