dozc58418381 2013-02-13 20:47
浏览 46
已采纳

从FPDF创建多个PDF文件

What I'm trying to do is create and email multiple PDF files using FPDF. I already have everything working to email a single file but when I try to stick everything into a loop, the script ends up outputting only the first instance of the PDF then exits to a blank screen. this runs perfectly when I'm only processing one record, however when you add multiple, it only runs a single time. I really can't figure out what to do here is my code. Any assistance or direction is appreciated.

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 
        }
    }

Here is the gist of my pdf generating code, the specifics of the generation left out.

<?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条回答 默认 最新

  • douao3063 2013-02-13 21:12
    关注

    Your approach is correct, but you may need to restructure your logic.

    In each of the files you include, you would want to write functions that to the work of generating the PDF and sending the email. Then your loop can be redone to look like this:

    require("generatepdf.php"); 
    require("ledgeremail.php");
    
    while ($data = mysql_fetch_row($query)) // under normal circumstances working properly
    {
        $clientid = $data['0'];
        if($clientid)
        {
            $cname = generate_pdf_for_client( $clientid ); // calls a function in generatepdf.php
            $savepath = 'c:/ledgers/'.$cname.' Cash Ledger.pdf';
            $pdf->Output($savepath,'F');
            send_email_to_client(some appropriate parameters); // calls a function in ledgeremail.php
        }
    }
    

    So, in your files, you need to implement the logic for the proposed generate_pdf_for_client and send_email_to_client functions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算