Php 从FPDF创建多个PDF文件

Php 从FPDF创建多个PDF文件,php,pdf,fpdf,Php,Pdf,Fpdf,我正在尝试使用FPDF创建和发送多个PDF文件。我已经可以通过电子邮件发送单个文件了,但是当我尝试将所有内容都放入一个循环时,脚本最终只输出PDF的第一个实例,然后退出到一个空白屏幕。当我只处理一条记录时,它运行得很好,但是当你添加多条记录时,它只运行一次。我真的不知道该怎么做这里是我的代码。任何帮助或指导都将不胜感激 while ($data = mysql_fetch_row($query)) // under normal circumstances working properly

我正在尝试使用FPDF创建和发送多个PDF文件。我已经可以通过电子邮件发送单个文件了,但是当我尝试将所有内容都放入一个循环时,脚本最终只输出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('');
        }                   
    }
}

?>

您的方法是正确的,但您可能需要重新构建逻辑

在您包含的每个文件中,您都希望编写一些函数,用于生成PDF和发送电子邮件。然后可以将循环重做为如下所示:

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

因此,在您的文件中,您需要为建议的
generate\u pdf\u for\u client
send\u email\u to\u client
功能实现逻辑。

您的方法是正确的,但您可能需要重新构造逻辑

在您包含的每个文件中,您都希望编写一些函数,用于生成PDF和发送电子邮件。然后可以将循环重做为如下所示:

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

因此,在您的文件中,您需要实现建议的
generate_pdf_for_client
send_email_to_client
函数的逻辑。

好的,我创建了这里列出的函数,但是当我运行它们时,它们不会像以前那样输出任何东西。可能
$pdf->Output($savepath,'F')
在前面的代码中没有创建$pdf实例时,实际上不知道要输出什么?当我将代码放入函数中时,它仍将处理第一个分类账,但不会继续处理下一个记录。我开始认为这是
$pdf
的一个问题,在继续循环之前,我已尝试取消设置该实例,但仍然没有成功。如果看不到pdf生成器的功能,很难判断发生了什么。答案中建议的要点是封装每个任务的功能职责。您可以从generate_PDF调用返回PDF对象,而不是每次都重复使用单个实例。我添加了我的pdf生成器信息,它主要是fpdf类的样式功能是的。我在家里有个主意,明天回去工作时我要去测试一下。我将尝试背靠背输出两个简单的fpdf文档,看看是fpdf本身还是我的代码有问题……感谢您的反馈。我最终重写了所有内容,学习oo编程知识并封装了这些函数。现在可以了。好吧,我创建了这里列出的函数,但是当我运行它们时,它们没有像以前那样输出任何东西。可能
$pdf->Output($savepath,'F')
在前面的代码中没有创建$pdf实例时,实际上不知道要输出什么?当我将代码放入函数中时,它仍将处理第一个分类账,但不会继续处理下一个记录。我开始认为这是
$pdf
的一个问题,在继续循环之前,我已尝试取消设置该实例,但仍然没有成功。如果看不到pdf生成器的功能,很难判断发生了什么。答案中建议的要点是封装每个任务的功能职责。您可以从generate_PDF调用返回PDF对象,而不是每次都重复使用单个实例。我添加了我的pdf生成器信息,它主要是fpdf类的样式功能是的。我在家里有个主意,明天回去工作时我要去测试一下。我将尝试背靠背输出两个简单的fpdf文档,看看是fpdf本身还是我的代码有问题……感谢您的反馈。我最终重写了所有内容,学习oo编程知识并封装了这些函数。现在可以了。嗨,杰瑞德·梅林,你解决问题了吗?请问你有完整的密码吗?我想使用fdpdf创建并通过电子邮件发送多个pdf。嗨,Jared Meyering,你解决问题了吗?请问你有完整的密码吗?我想创建并通过电子邮件发送多个pdf使用fdpdf。