2013-02-13 111 views
1

我想要做的是使用FPDF創建並通過電子郵件發送多個PDF文件。我已經有一切工作可以通過電子郵件發送單個文件,但是當我嘗試將所有內容粘貼到循環中時,腳本最終只輸出PDF的第一個實例,然後退出到空白屏幕。當我只處理一條記錄時,它會完美運行,但是當您添加多條記錄時,它只會運行一次。我真的不知道在這裏做什麼是我的代碼。任何幫助或方向表示讚賞。從FPDF創建多個PDF文件

while ($data = mysql_fetch_row($query)) // under normal circumstances working properly 
    { 
     $clientid = $data['0']; 
     if($clientid) 
     { 
      require("generatepdf.php"); //styles the pdf and is working properly 
      $savepath = 'c:/ledgers/'.$cname.' Cash Ledger.pdf'; 
      $pdf->Output($savepath,'F'); 
      require("ledgeremail.php"); //runs query to get email address and emails the outputted file 
     } 
    } 

這是我的pdf生成代碼的要點,遺漏的一代的具體細節。

<?php 

require("includes/pdf/fpdf.php"); 


{ 

$pdf = new FPDF(); 
$pdf->AddPage(); 
$pdf->SetFont('Arial','',16); 

    //Run Query to get Ledger information for the $clientid variable 
      //Pass information from query into the different variables throughout script 



      //Print Document Title 
      $pdf->Cell(0,10,$cname.' Petty Cash Ledger', 0,1,'L'); 
      //Print First Month Grouping 
      $pdf->SetFont('Arial','',10); 
      $pdf->Line($linestart,$pdf->GetY(),$lineend,$pdf->GetY()); 
      $pdf->Cell(10,5,$startmonth.", ".$startyear, 0,0,'L'); 
      $pdf->SetX(175); 
      $pdf->SetFont('','B'); 
      $pdf->Cell(20,5,'Balance Forward: $'.$balforward, 0,1,'R'); 
      $pdf->SetFont(''); 
      //print group headers 
      $pdf->SetTextColor(101,102,102); 
      $pdf->Cell(30,5,'Date',0,0,'L'); 
      $pdf->SetX(30); 
      $pdf->Cell(105,5,'Description', 0,0,'L'); 
      $pdf->SetX(135); 
      $pdf->Cell(20,5,'Deposit', 0,0,'R'); 
      $pdf->SetX(155); 
      $pdf->Cell(20,5,'Withdrawl', 0,0,'R'); 
      $pdf->SetX(175); 
      $pdf->Cell(20,5,'Balance', 0,1,'R'); 
      $pdf->SetTextColor(0,0,0); 
      $pdf->SetFillColor(255); 

      while($ledger = mysql_fetch_row($ledgerqry)) 
      { 
       if ($grey == 1) $pdf->SetFillColor(225); 

       $currentmonth = date('F',strtotime($data[0])); 
       if ($currentmonth != $startmonth) 
       { 
        $pdf->SetFillColor(255); 
        $grey = 0; 
        $currentyear = date('Y',strtotime($data[0])); 
        //Print Month End Balance 
        $pdf->SetX(175); 
        $pdf->SetFont('','B'); 
        $pdf->Cell(20,5,'Ending Balance: '.$runningbal, 0,1,'R',1); 
        $pdf->SetFont(''); 
        $pdf->Line($linestart,$pdf->GetY()+2,$lineend,$pdf->GetY()+2); 
        //Print Month Grouping 
        $pdf->Cell(10,10,$currentmonth.", ".$currentyear, 0,0,'L'); 
        $pdf->SetX(175); 
        $pdf->SetFont('','B'); 
        $pdf->Cell(20,10,"Balance Forward: ".$runningbal, 0,1,'R'); 
        $pdf->SetFont(''); 


        //print group headers 
        $pdf->SetTextColor(101,102,102); 
        $pdf->Cell(30,5,'Date',0,0,'L'); 
        $pdf->SetX(30); 
        $pdf->Cell(105,5,'Description', 0,0,'L'); 
        $pdf->SetX(135); 
        $pdf->Cell(20,5,'Deposit', 0,0,'R'); 
        $pdf->SetX(155); 
        $pdf->Cell(20,5,'Withdrawl', 0,0,'R'); 
        $pdf->SetX(175); 
        $pdf->Cell(20,5,'Balance', 0,1,'R'); 
        $pdf->SetTextColor(0,0,0); 

        $startmonth = $currentmonth; 
       } 

       //Create line Variables 
       $tdate = date('m/d/Y',strtotime($ledger[0])); 
       $tdescription = $ledger[2]; 
       if($ledger[3]==0) $tdeposit = ""; else $tdeposit = "$".number_format($ledger[3], 2, '.', ','); 
       if($ledger[4]==0) $twithdrawl = ""; else $twithdrawl = "($".-1*number_format($ledger[4], 2, '.', ',').")"; 
       $runningbal = "$".number_format($balforward + $ledger[5], 2, '.', ','); 

       $pdf->Cell(30,7,$tdate, 0,0,'L',1); 
       $pdf->SetX(30); 
       $pdf->Cell(105,7,$tdescription, 0,0,'L',1); 
       $pdf->SetX(135); 
       $pdf->Cell(20,7,$tdeposit, 0,0,'R',1); 
       $pdf->SetX(155); 
       $pdf->Cell(20,7,$twithdrawl, 0,0,'R',1); 
       $pdf->SetX(175); 
       $pdf->Cell(20,7,$runningbal, 0,1,'R',1); 

       if ($grey == 1) 
       { 
        $pdf->SetFillColor(255); 
        $grey = 0; 
       } 
       else $grey = 1; 



      } 
      //Create Final balance 
      $pdf->SetFillColor(255); 
      $pdf->SetX(175); 
      $pdf->SetFont('','B'); 
      $pdf->Cell(20,5,'Ending Balance: '.$runningbal, 0,1,'R',1); 
      $pdf->SetFont(''); 
     }     
    } 
} 

?> 

回答

1

您的方法是正確的,但您可能需要重構您的邏輯。

在您包含的每個文件中,您都希望編寫用於生成PDF和發送電子郵件的函數。然後,你的循環可以重做,看起來像這樣:

所以,在你的文件,你需要實現邏輯擬議generate_pdf_for_clientsend_email_to_client功能。

+0

好吧,我創建了這裏列出的功能,但是當我運行它們時,它們不會像以前那樣輸出任何東西。可能'$ pdf->輸出($ savepath,'F')'實際上不知道在實際的前面的代碼中創建$ pdf實例時沒有輸出什麼?當我將該代碼放入函數中時,它將仍然處理第一個分類帳,但不會移動到下一個記錄。我開始認爲它是'$ pdf'的問題我試圖在繼續循環之前取消設置實例,但仍然沒有運氣。 – 2013-02-13 21:57:12

+0

沒有看到你的PDF生成器做什麼,很難說出發生了什麼。答案中的建議的要點是要封裝每項任務的職能責任。您可能會從generate_pdf調用中返回PDF對象,而不是每次重新使用一個實例。看起來就像你在做什麼。 – Ryan 2013-02-13 22:00:39

+0

我添加了我的pdf生成器信息,其主要是fpdf類的樣式函數 – 2013-02-13 22:21:34