2015-04-02 79 views
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); 
} 
?> 

回答

0

答案是错误消息:只需推动类的声明之外你的while循环。

相关问题