PHP TCPDF 多个 WriteHTML() 不起作用
我编写了一些代码来创建报告,然后使用 TCPDF 将其导出为 .PDF。 我想将报告的不同部分发送到邮件,因此我需要多次使用函数 WriteHTML() 以创建多个 pdf 文件并将它们保存在特定目录中。
$parts=explode("<div class='subTable'>",$html);
pdf->writeHTML($html, true, false, false, false, '');
$pdf->Output('exmpl/example_045.pdf', 'I');
$pdf->Output('exmpl/example_046.pdf', 'F');
$pdf->writeHTML($parts[2], true, 0, true, 0);
//Close and output PDF document
$pdf->Output('exmpl/example_048.pdf', 'F');
尽管我尝试将两个不同的文件导出到目录 exmpl ,但我实际上得到的只是一个文件(example_046.pdf)。
我不得不提的是,我可以多次使用 Output() 并且我已经做过这样的事情,但我只是不能多次使用 writeHTML() 。
任何解决方案将不胜感激, 提前致谢 !
编辑
这是完整的代码:
<?php
function createPDF($html, $type, $filename, $output,$saleStart,$saleEnd,$reportType)
{
//REQUIRE TCPDF
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/config/lang/heb.php');
require_once('tcpdf/tcpdf.php');
class MYPDF extends TCPDF {
//Page header
public function Header() {
// Set font
$this->SetFont('helvetica', 'B', 20);
// Title
$this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M');
}
// Page footer
public function Footer() {
// Position at 15 mm from bottom
$this->SetY(-15);
// Set font
$this->SetFont('helvetica', 'I', 8);
// Page number
$this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
/*
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 018');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
*/
// SET CUSTOMIZED DATE INCLUDED HEADER
$headerSDate=reverseLongDate($saleStart);
$headerEDate=reverseLongDate($saleEnd);
$header="";
$header.="\t";
$header.=Date("d-m-Y h:m:s");
$header.="\n";
$header.='מועדון המגדלים יוסי חותה פירות וירקות';
$header.="\n";
if($reportType=='customer')
{
$header.='דו"ח שיווק ללקוח לתאריכים '.$headerSDate." עד ".$headerEDate;
}
else
if($reportType=='newCustomer')
{
$header.='דו"ח שיווק ללקוח לתאריכים '.$headerSDate." עד ".$headerEDate;
}
else
if($reportType=='collection')
{
$header.='דו"ח גביה ללקוח לתאריכים '.$headerSDate." עד ".$headerEDate;
}
else
if($reportType=='grower')
{
$header.='דו"ח מגדל ללקוח לתאריכים '.$headerSDate." עד ".$headerEDate;
}
$pdf->SetHeaderData("","" ,"",$header);
//$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
// set header and footer fonts
$pdf->setHeaderFont(Array("dejavusans", '', "15"));
$pdf->setFooterFont(Array("dejavusans", '', "12"));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(0);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language dependent data:
$lg = Array();
$lg['a_meta_charset'] = 'UTF-8';
$lg['a_meta_dir'] = 'rtl';
$lg['a_meta_language'] = 'fa';
$lg['w_page'] = 'page';
//set some language-dependent strings
$pdf->setLanguageArray($lg);
// ---------------------------------------------------------
// set font
$pdf->SetFont('dejavusans', '', 12);
// add a page
$pdf->AddPage();
// -----------------------------------------------------------------------------
if($type == "all"){
$pdf->writeHTML($html, true, false, false, false, '');
} elseif($type == "partial"){
$parts = explode("<div class='subTable'>", $html);
$pdf->writeHTML($parts[2], true, 0, true, 0);
}
ob_clean();
$pdf->Output('exmpl/' . $filename . '.pdf', $output);
} //END_OF_FUNCTION
set_time_limit(0);
require('modifyDate.php');
require('createQuery.php');
//GET FORM DATA
if(isset($_POST['submitMeshavek']))
{
$saleStart=$_POST['saleStart'];
$saleEnd=$_POST['saleEnd'];
$saleWeek=$_POST['weekId'];
$growerId=$_POST['growerId'];
$reportType=$_POST['reportType'];
}
/*
$regularSaleStart=reverseDate($saleStart);
$regularSaleEnd=reverseDate($saleEnd);
$header='<h2>מועדון מגדלים יוסי חותה פירות וירקות</h2>';
$header.="דוח שיווק ללקוח לתאריכים ".$regularSaleEnd." ".$regularSaleStart;
public function Header() {
// Set font
$this->SetFont('dejavusans', '', 10);
// Title
$this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M');
}
*/
if($reportType=='customer')
{
include('reportType/customer.php');
}
else
if($reportType=='newCustomer')
{
include('reportType/customerNew.php');
}
else
if($reportType=='collection')
{
include('reportType/collect.php');
}
else
if($reportType=='grower')
{
include('reportType/grower.php');
}
createPDF($html, "all", "example_045", "I",$saleStart,$saleEnd,$reportType);
createPDF($html, "partial", "example_048", "F",$saleStart,$saleEnd,$reportType);
createPDF($html, "all", "example_046", "F",$saleStart,$saleEnd,$reportType);
/*
ob_clean();
//$pdf->writeHTML($html, true, false, false, false, '');
$pdf->writeHTML($html, true, 0, true, 0);
//Close and output PDF document
$pdf->Output('example_048.pdf', 'I');
$pdf->writeHTML($html, true, 0, true, 0);
//Close and output PDF document
$pdf->Output('examp.pdf', 'I');
*/
?>
I've written some code which created reports and then exports it to .PDF using TCPDF.
I want to send different parts of the report to mail , therefor I need to use the function WriteHTML() more than once in order to create multiple pdf files and save them in a specific directory.
$parts=explode("<div class='subTable'>",$html);
pdf->writeHTML($html, true, false, false, false, '');
$pdf->Output('exmpl/example_045.pdf', 'I');
$pdf->Output('exmpl/example_046.pdf', 'F');
$pdf->writeHTML($parts[2], true, 0, true, 0);
//Close and output PDF document
$pdf->Output('exmpl/example_048.pdf', 'F');
Altough I try to export two different files to the directory exmpl , all I actually get is one file (example_046.pdf).
I have to mention that I can use Output() more than once and I've already done such thing, but I just can't use writeHTML() more than once.
Any solution will be appreciated,
Thanks in advance !
EDIT
Here's the full code :
<?php
function createPDF($html, $type, $filename, $output,$saleStart,$saleEnd,$reportType)
{
//REQUIRE TCPDF
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/config/lang/heb.php');
require_once('tcpdf/tcpdf.php');
class MYPDF extends TCPDF {
//Page header
public function Header() {
// Set font
$this->SetFont('helvetica', 'B', 20);
// Title
$this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M');
}
// Page footer
public function Footer() {
// Position at 15 mm from bottom
$this->SetY(-15);
// Set font
$this->SetFont('helvetica', 'I', 8);
// Page number
$this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
/*
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 018');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
*/
// SET CUSTOMIZED DATE INCLUDED HEADER
$headerSDate=reverseLongDate($saleStart);
$headerEDate=reverseLongDate($saleEnd);
$header="";
$header.="\t";
$header.=Date("d-m-Y h:m:s");
$header.="\n";
$header.='מועדון המגדלים יוסי חותה פירות וירקות';
$header.="\n";
if($reportType=='customer')
{
$header.='דו"ח שיווק ללקוח לתאריכים '.$headerSDate." עד ".$headerEDate;
}
else
if($reportType=='newCustomer')
{
$header.='דו"ח שיווק ללקוח לתאריכים '.$headerSDate." עד ".$headerEDate;
}
else
if($reportType=='collection')
{
$header.='דו"ח גביה ללקוח לתאריכים '.$headerSDate." עד ".$headerEDate;
}
else
if($reportType=='grower')
{
$header.='דו"ח מגדל ללקוח לתאריכים '.$headerSDate." עד ".$headerEDate;
}
$pdf->SetHeaderData("","" ,"",$header);
//$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
// set header and footer fonts
$pdf->setHeaderFont(Array("dejavusans", '', "15"));
$pdf->setFooterFont(Array("dejavusans", '', "12"));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(0);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language dependent data:
$lg = Array();
$lg['a_meta_charset'] = 'UTF-8';
$lg['a_meta_dir'] = 'rtl';
$lg['a_meta_language'] = 'fa';
$lg['w_page'] = 'page';
//set some language-dependent strings
$pdf->setLanguageArray($lg);
// ---------------------------------------------------------
// set font
$pdf->SetFont('dejavusans', '', 12);
// add a page
$pdf->AddPage();
// -----------------------------------------------------------------------------
if($type == "all"){
$pdf->writeHTML($html, true, false, false, false, '');
} elseif($type == "partial"){
$parts = explode("<div class='subTable'>", $html);
$pdf->writeHTML($parts[2], true, 0, true, 0);
}
ob_clean();
$pdf->Output('exmpl/' . $filename . '.pdf', $output);
} //END_OF_FUNCTION
set_time_limit(0);
require('modifyDate.php');
require('createQuery.php');
//GET FORM DATA
if(isset($_POST['submitMeshavek']))
{
$saleStart=$_POST['saleStart'];
$saleEnd=$_POST['saleEnd'];
$saleWeek=$_POST['weekId'];
$growerId=$_POST['growerId'];
$reportType=$_POST['reportType'];
}
/*
$regularSaleStart=reverseDate($saleStart);
$regularSaleEnd=reverseDate($saleEnd);
$header='<h2>מועדון מגדלים יוסי חותה פירות וירקות</h2>';
$header.="דוח שיווק ללקוח לתאריכים ".$regularSaleEnd." ".$regularSaleStart;
public function Header() {
// Set font
$this->SetFont('dejavusans', '', 10);
// Title
$this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M');
}
*/
if($reportType=='customer')
{
include('reportType/customer.php');
}
else
if($reportType=='newCustomer')
{
include('reportType/customerNew.php');
}
else
if($reportType=='collection')
{
include('reportType/collect.php');
}
else
if($reportType=='grower')
{
include('reportType/grower.php');
}
createPDF($html, "all", "example_045", "I",$saleStart,$saleEnd,$reportType);
createPDF($html, "partial", "example_048", "F",$saleStart,$saleEnd,$reportType);
createPDF($html, "all", "example_046", "F",$saleStart,$saleEnd,$reportType);
/*
ob_clean();
//$pdf->writeHTML($html, true, false, false, false, '');
$pdf->writeHTML($html, true, 0, true, 0);
//Close and output PDF document
$pdf->Output('example_048.pdf', 'I');
$pdf->writeHTML($html, true, 0, true, 0);
//Close and output PDF document
$pdf->Output('examp.pdf', 'I');
*/
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题是在
$pdf->Output('exmpl/example_045.pdf', 'I')
之后,就好像脚本在那里结束一样。为什么要射击到文件头。你不能拍摄两个不同的头文件,如果你想生成两个不同的pdf,我建议你使用函数输出字符串,
因此文件系统上的这两个文件保存它们并拍摄。
压缩文件并拍摄包含两个 pdf 的压缩文件头。
在你的情况下更新
我会使用类似的方法:
最终更新
正确的代码:
Your problem is that after the
$pdf->Output('exmpl/example_045.pdf', 'I')
, and as if the script ends there. Why shoot to the file header.You can not shoot at two different header files, if you want to generate two distinct pdf I recommend you go with the function outputs the string
thus these two files on the filesystem save them and shoot.
Compress the files and shoot the zipped file header containing the two pdf.
UPDATE
in your case I would use a similar approach:
Final Update
right code:
来自 tcpdf 文档
因此您需要创建另一个
$pdf
实例,因为在Output()
未终止后再次使用writeHTML()
from tcpdf docs
so you need to create another
$pdf
instance 'cause afterOutput()
is't terminated to usewriteHTML()
again