如何使用mpdf API创建多个pdf文件。
当我尝试使用mpdf创建时,我收到了错误消息,如
致命错误:无法重新声明类mPDF
$sql1=mysql_query("SELECT * FROM $tbl_name");
while($row=mysql_fetch_array($sql1))
{
$Alert_date=$row['Alert_Mail_Date'];
$cart_body='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New Order Placed</title>
</head>
<body>
<table width="550" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="400" align="left" valign="top">
<a style="font-size:16px"><strong>.$Alert_date.</strong></a><br />
</body>
</html>';
include("APIs/mpdf/mpdf.php");
// Define a new mPDF document using utf-8 fonts
$mpdf=new mPDF('utf-8');
$ran= rand(0,9999999);
//$mpdf = new mPDF('ta',array(210,297)); //array(210,297) --> A4 Size..
$mpdf->SetAutoFont(AUTOFONT_ALL,array(210,297));
$mpdf->WriteHTML($cart_body);
$filename="/opt/lampp/htdocs/Invoice/invoice_PDF/".$ran."test.pdf";
$mpdf->Output($filename,'F');
//$mpdf->Output();
}
答案 0 :(得分:1)
错误只是意味着你有一个同名的类多次声明。也许通过多个包括。
在循环之前使用include
语句。
同时查看include_once。它会在下次保存。
答案 1 :(得分:0)
您继续重新声明mpdf。 而是在循环之前声明它。
include("APIs/mpdf/mpdf.php");
$sql1=mysql_query("SELECT * FROM $tbl_name");
while($row=mysql_fetch_array($sql1))
{
$Alert_date=$row['Alert_Mail_Date'];
$cart_body='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New Order Placed</title>
</head>
<body>
<table width="550" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="400" align="left" valign="top">
<a style="font-size:16px"><strong>.$Alert_date.</strong></a><br />
</body>
</html>';
// Define a new mPDF document using utf-8 fonts
$mpdf=new mPDF('utf-8');
$ran= rand(0,9999999);
//$mpdf = new mPDF('ta',array(210,297)); //array(210,297) --> A4 Size..
$mpdf->SetAutoFont(AUTOFONT_ALL,array(210,297));
$mpdf->WriteHTML($cart_body);
$filename="/opt/lampp/htdocs/Invoice/invoice_PDF/".$ran."test.pdf";
$mpdf->Output($filename,'F');
//$mpdf->Output();
}
答案 2 :(得分:0)
是的,您可以使用mpdf生成多个pdf,您只需将pdf生成代码放在循环中。由于必须在不关闭前一个实例的情况下启动新实例,因此发生了类错误的重新声明。 $mpdf->Output
结束实例。尝试在此声明后重新定义类。
将include("APIs/mpdf/mpdf.php");
保持在循环之外。
答案 3 :(得分:0)
下面是生成多个PDF文件并将其保存在某个目录中的代码。我使用sleep和time函数生成唯一的文件名,以便它覆盖同名的现有文件。
<?php
$record = array('BMW','Mercedes','Toyota');
include_once("../lib/MPDF57/mpdf.php");
foreach($record as $value){
$filename = time().'Performance_review.pdf';
sleep(2);
$mpdf=new mPDF();
$mpdf->WriteHTML($value);
$get_pdf = $mpdf->Output('../pdf-output/'.$filename,'F');
}?>