FPDF 生成单独的页面而不是一个大页面

FPDF Generate individual pages instead of one big One

我正尝试在 fpdf 上为数组中的每封电子邮件生成多页,但它一直为每封电子邮件生成一个包含数据的长页面,并将该长页面发送给每个人。 更新 这是我的全部代码……第一部分生成 PDF,第二部分通过电子邮件发送。

<?php
while (ob_get_level())
ob_end_clean();
header("Content-Encoding: None", true);

//Requires here     
require('../fpdf/fpdf.php');
require_once '../connect.php';

$db = new PDO('mysql:host=localhost;dbname=mcle','user','pass'); 
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// CREATE AND SAVE THE PDF DOCUMENT
class pdf extends FPDF
{

function header()
{

//$this->image('img/lsnc_logo.jpg',2,2);
$this->SetTextColor(0,0,255);
$this->SetFont('Helvetica','B',16);
$this->SetFillColor('230,230,230');
$this->SetXY(35,15); 
$this->Cell(140,10,'Certificate Of Attendance for California 
MCLE',1,0,'C', TRUE);
$this->Ln(20);
} 
function pdf_body($db)
{   
$emails = $_POST['email']; 
foreach ($emails as $email)
{
$stmt = $db->query("SELECT * 
FROM sessions JOIN attendance 
ON sessions.event_id = attendance.event_id JOIN staff 
ON attendance.staff_bar_no = staff.staff_bar_no
WHERE staff.email = '$email'");
for($i = 0; $i < count($emails); $i++)
{

while($data = $stmt->fetch(PDO::FETCH_OBJ))
{

$this->Ln(10);
$this->SetX(20);
$this->SetFont('Arial','B',11);             
$this->Cell(45,6,'Provider Name:',0,0,'R',0);

$this->SetFont('Times','',11);
$this->Cell(150,6,'Legal Services Of Northern California',0,0,'L');
$this->Ln();

$this->SetX(20);
$this->SetFont('Arial','B',11);             
$this->Cell(45,6,'Provider Number:',0,0,'R',0);
$this->SetFont('Times','',11);
$this->Cell(150,6,'2221',0,0,'L');
$this->Ln();

$this->SetX(20);
$this->SetFont('Arial','B',11);             
$this->Cell(45,6,'Title Of Activity:',0,0,'R',0);

$this->SetFont('Times','',11);
$this->Cell(150,6,$data->pro_title,0,0,'L');
$this->Ln();

$this->SetX(20);
$this->SetFont('Arial','B',11); 
$this->Cell(45,6,'Date Of Activity:',0,0,'R',0);
$this->SetFont('Times','',11);
$this->Cell(65,6,$data->date,0,0,'L');
$this->Ln();                

$this->SetX(20);
$this->SetFont('Arial','B',11); 
$this->Cell(45,6,'Time of Activity:',0,0,'R',0);
$this->SetFont('Times','',11);
$this->Cell(65,6,$data->time,0,0,'L');
$this->Ln();

$this->SetX(20);
$this->SetFont('Arial','B',11); 
$this->Cell(45,6,'Location (City, State):',0,0,'R',0);
$this->SetFont('Times','',11);
$this->Cell(150,6,$data->location,0,0,'L');
$this->Ln();


$this->Ln(10);
$this->SetFont('Arial','B',12);
$this->Cell(65,6,'Eligible California MCLE Credit','',0);
$this->Ln(10);

$this->SetFont('Times','',12);
$this->Cell(109,6,'Total California MCLE Credit Hours for the above 
activity are:',0,0,'R',0);
$this->SetFont('Times','B',12);
$this->Cell(4,6,$data->length,0,0,'R');
$this->SetFont('Times','',12);
$this->Cell(80,6,', including the following sub-field 
credits:',0,0,'L',0);
$this->Ln(5);

$this->SetFont('Arial','',12);  
$this->Cell(45,6,'* Legal Ethics: ',0,0,'R',0);
$this->SetFont('Arial','B',12); 
$this->Cell(45,6,$data->length,0,0,'L');
$this->Ln();


$this->SetFont('Arial','',12);  
$this->Cell(57,6,'* Elimination of Bias: ',0,0,'R',0);
$this->SetFont('Arial','B',12);
$this->Cell(45,6,$data->bias,0,0,'L');
$this->Ln();

$this->SetFont('Arial','',12);  
$this->Cell(174,6,'* Substance Abuse / Mental or Physical Issues Affecting 
Professional Competence: ',0,0,'R',0);
$this->SetFont('Arial','B',12);
$this->Cell(10,6,$data->sampiapc,0,0,'L');
$this->SetFont('Arial','',12);  


$this->Ln(15);  
$this->SetFont('Times','B',11);             
$this->MultiCell(174,6,'THE BOTTOM PORTION OF THIS FORM IS TO BE COMPLETED 
BY THE ATTORNEY AFTER PARTICIPATION IN THE ABOVE-REFERENCED ACTIVITY');

$this->Ln(12);
// Split Here 
$this->SetFont('Times','I',12);     
$this->SetX(20);
$this->MultiCell(170, 6, '' . str_repeat('By signing below, I certify that 
I participated in all or some* of the activity described above and am 
therefore entitled to the following MCLE credit hours -', 1),'LRT');

$this->SetFont('Times','',12);
$this->SetX(20);
$this->Cell(170,6,'Total California MCLE Credit Hours ______, including 
the following sub-field credits:','LR');

$this->Ln();
$this->SetX(20);
$this->SetFont('Times','',12);  
$this->Cell(170,6,'         Legal Ethics:______','LR');
$this->Ln();

$this->SetX(20);            
$this->SetFont('Times','',12);  
$this->Cell(170,6,'         Elimination of Bias:______','LR');
$this->Ln();

$this->SetX(20);
$this->SetFont('Times','',12);              
$this->Cell(170,6,'         Substance Abuse / Mental or Physical Issues 
Affecting Professional Competence:____ ','LR');
$this->Ln();


$this->SetFont('Times','I',12); 
$this->SetX(20);    
$this->Cell(170,6,' ','LR');
$this->Ln();
$this->SetX(20);                
$this->MultiCell(170,6,'*Partial participation hours must be pro-rated. 
You may not claim credit for sub-fields unless the Provider is granting 
credit in those areas and you participated in those portions of the 
activity ','LR');

$this->SetX(20);
$this->Cell(170,6,' ','LR');
$this->Ln();

//
$this->SetX(20);
$this->SetFont('Times','',12);  
$this->Cell(33,6,'Print Your Name:',0,'L');
$this->SetFont('Arial','B',12);
$this->Cell(80,6,$data->fname." ".$data->lname ,0,0,'L');

$this->SetFont('Arial','',12);
$this->Cell(28,6,'State Bar No: ',0,'R');
$this->SetFont('Arial','B',12);

$this->Cell(29,6,$data->staff_bar_no,0,0,'L');

$this->SetX(20);
$this->Cell(170,10,' ','LR');
//
$this->Ln();
$this->SetX(20);
$this->SetFont('Times','',12);  
$this->Cell(170,6,'Your Signature:_____________________________ ','LR');

$this->SetX(20);
$this->Cell(170,6,' ','LR');
$this->Ln();
$this->SetX(20);
$this->Cell(170,6,' ','LRB');
$this->Ln(19);


$this->SetFont('Times','B',10); 
$this->SetX(20);
$this->Cell(25,4,'Reminders: ',0,0,'L',0);
$this->Ln();
$this->SetX(20);
$this->SetFont('Times','',10);  
$this->MultiCell(170,4,'1- Keep this record of attendance for 4 years. In 
the event that you are audited by the State Bar, you may be requested to 
submit this record of attendance. Send this to the State Bar only if you 
are audited');
$this->SetX(20);
$this->MultiCell(170,4,'2- You must personally sign-in on the Official 
Record of Attendance for California MCLE or complete a contemporaneous 
registered login online for web events maintained by this provider in 
order to qualify for California MCLE credits.');

}       

}   
}

}

function footer()
{

}
//////////////////////////      Make sure we don't detete this below
}   
//////////////////////////
$pdf = new pdf();
$pdf->AddPage('P','A4',0);
$pdf->pdf_body($db);

//$pdf->Output();
//Mailing Parts here 

$emails = $_POST['email']; //This is mainly to declare $email 
foreach ($emails as $email)
{
//
}

// email stuff (change data below)
$to = $email;
$from = "noreply@domain"; 
$subject = "Your LSNC MCLE Attendance Certificate"; 
$message = "<p>Hello,</p> <p>Here is your LSNC MCLE Attendance 
Certificate. Please Confirm . Thank you !</p>";

// a random hash will be necessary to send mixed content
$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name 
$filename = "MCLE.pdf";

// encode data (puts attachment in proper format)                   
$pdfdoc = $pdf->Output('', 'S');
$attachment = chunk_split(base64_encode($pdfdoc));

// main header
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

// no more headers after this, we start the body! //


$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
//$body .= "This is a MIME encoded message.".$eol;

// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;

// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; 
name=\"".$filename."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
mail($to, $subject, $body, $headers) ;
unset($pdf);  // THIS will now ONLY send the first email with other 
//generated pages...

?>
<script>
alert('Emails Successfully Sent !');
history.back();
</script>
<?php
?>

我希望为此找到解决方案,我知道我在第一个循环中遗漏了,我无法遍历数组的元素(我认为)。一旦我做对了,电子邮件部分就会正常工作,因为我已经在个人记录上对其进行了测试...... 非常感谢@Dave。

你有一个 foreach 经历了 $_POST['multi1'] 和一个看起来相似的 foreach 经历了 $_POST['email'] 所以不清楚这些变量中的哪一个实际上来自您的表格(未显示)。

对于这个答案,我假设 $_POST['multi1'] 是要使用的正确变量。如果它不是根据需要调整代码。您会注意到,PDF 是为每个电子邮件地址构建、发送的,然后 PDF 对象在继续发送到下一个电子邮件地址之前被销毁。

// this stuff only needs to be set once
$subject = 'Your LSNC MCLE Attendance Certificate';
$message = '<p>Hello,</p><p>Here is your LSNC MCLE Attendance Certificate. Please Confirm . Thank you!</p>';

// a random hash will be necessary to send mixed content
$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = "MCLE.pdf";

if (isset($_POST['submit'])) {

    foreach($_POST['multi1'] as $email) {

        $to = $email;

        $stmt = $db->query("SELECT * FROM sessions
                            JOIN attendance ON sessions.event_id = attendance.event_id
                            JOIN staff      ON attendance.staff_bar_no = staff.staff_bar_no
                            WHERE staff.email = '$email'");

        while ($data = $stmt->fetch(PDO::FETCH_OBJ)) {
            //building pdf here
        }

        $pdf = new pdf();
        $pdf->AddPage('P','A4',0);
        $pdf->pdf_body($db);

        // encode data (puts attachment in proper format)

        $pdfdoc = $pdf->Output("", "S");
        $attachment = chunk_split(base64_encode($pdfdoc));

        // main header
        $headers  = 'From: noreply@emailaddress.domain' . $eol;
        $headers .= '"MIME-Version: 1.0' . $eol;
        $headers .= 'Content-Type: multipart/mixed; boundary="' . $separator .'"';

        // no more headers after this, we start the body! //

        $body  = '--' . $separator . $eol;
        $body .= 'Content-Transfer-Encoding: 7bit' . $eol . $eol;
        //$body .= "This is a MIME encoded message.".$eol;

        // message
        $body .= '--' . $separator . $eol;
        $body .= 'Content-Type: text/html; charset="iso-8859-1"' . $eol;
        $body .= 'Content-Transfer-Encoding: 8bit' . $eol . $eol;
        $body .= $message . $eol;

        // attachment
        $body .= '--' . $separator . $eol;
        $body .= 'Content-Type: application/octet-stream; name="MCLE.pdf"' . $eol;
        $body .= 'Content-Transfer-Encoding: base64' . $eol;
        $body .= 'Content-Disposition: attachment' . $eol . $eol;
        $body .= $attachment . $eol;
        $body .= '--' . $separator . '--';
        mail($to, $subject, $body, $headers) ;

        unset($pdf);  // destroy PDF object

    }  // end of foreach through $_POST['multi1']

}  // end of isset($_POST['submit'])

以下是最终对我有用的方法,感谢@Dave 的帮助。

<?php
while (ob_get_level())
ob_end_clean();
header("Content-Encoding: None", true);

global $idx; 
global $email; 

//Requires here     
require('../fpdf/fpdf.php');
require_once '../connect.php';

$db = new PDO('mysql:host=localhost;dbname=mcle','user','pass'); 
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// this stuff only needs to be set once
$subject = 'Your LSNC MCLE Attendance Certificate';
$message = '<p>Hello,</p><p>Here is your LSNC MCLE Attendance Certificate. Please 
Confirm . Thank you!</p>';

// a random hash will be necessary to send mixed content
$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = "MCLE.pdf";

if (isset($_POST['submit']))
{
foreach($_POST['email'] as $email)
{
$to = $email;
$stmt = $db->query("SELECT * 
FROM sessions JOIN attendance 
ON sessions.event_id = attendance.event_id JOIN staff 
ON attendance.staff_bar_no = staff.staff_bar_no
WHERE staff.email = '$email'");
while($data = $stmt->fetch(PDO::FETCH_OBJ))
{
//Header
//$pdf->image('img/lsnc_logo.jpg',2,2);
$pdf = new FPDF();
$pdf->AddPage('P','A4',0);
$pdf->SetTextColor(0,0,255);
$pdf->SetFont('Helvetica','B',16);
$pdf->SetFillColor('230,230,230');
$pdf->SetXY(35,15); 
$pdf->Cell(140,10,'Certificate Of Attendance for California MCLE',1,0,'C', TRUE);
$pdf->Ln(20);

//Body
$pdf->SetTextColor(0,0,0);
$pdf->Ln(10);
$pdf->SetX(20);
$pdf->SetFont('Arial','B',11);              
$pdf->Cell(45,6,'Provider Name:',0,0,'R',0);

$pdf->SetX(20);
$pdf->SetFont('Times','',12);   
$pdf->Cell(33,6,'Print Your Name:',0,'L');
$pdf->SetFont('Arial','B',12);
$pdf->Cell(80,6,$data->fname." ".$data->lname ,0,0,'L');

$pdf->SetFont('Arial','',12);
$pdf->Cell(28,6,'State Bar No: ',0,'R');
$pdf->SetFont('Arial','B',12);

$pdf->Cell(29,6,$data->staff_bar_no,0,0,'L');

$pdf->SetX(20);
$pdf->Cell(170,10,' ','LR');
//
$pdf->Ln();
$pdf->SetX(20);
$pdf->SetFont('Times','',12);   
$pdf->Cell(170,6,'Your Signature:_____________________________ ','LR');

}                                   
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));

// main header
$headers  = 'From: noreply@emailaddress here ' . $eol;
$headers .= '"MIME-Version: 1.0' . $eol;
$headers .= 'Content-Type: multipart/mixed; boundary="' . $separator .'"';

// no more headers after this, we start the body! //

$body  = '--' . $separator . $eol;
$body .= 'Content-Transfer-Encoding: 7bit' . $eol . $eol;
//$body .= "This is a MIME encoded message.".$eol;

// message
$body .= '--' . $separator . $eol;
$body .= 'Content-Type: text/html; charset="iso-8859-1"' . $eol;
$body .= 'Content-Transfer-Encoding: 8bit' . $eol . $eol;
$body .= $message . $eol;

// attachment
$body .= '--' . $separator . $eol;
$body .= 'Content-Type: application/octet-stream; name="MCLE.pdf"' . $eol;
$body .= 'Content-Transfer-Encoding: base64' . $eol;
$body .= 'Content-Disposition: attachment' . $eol . $eol;
$body .= $attachment . $eol;
$body .= '--' . $separator . '--';
mail($to, $subject, $body, $headers) ;

unset($pdf);  // destroy PDF object
?>
<script>
alert('Emails Successfully Sent !');
history.back();
</script>
<?php

} // end of foreach through $_POST['email']

}  // end of isset($_POST['submit'])/////////////
?>