加速TCPDF文件生成

时间:2011-10-28 12:16:57

标签: php html caching pdf tcpdf

我一直在使用TCPDF将HTML转换为pdf。

我注意到单页发票会在5秒内生成,而7页发票会在55秒内生成,原因我无法分辨。

我正在使用最新的TCPDF版本,我也尝试使用此解决方案并设置setFontSubsetting(false),但这些解决方案都没有改进TCPDF慢速文件生成。

这是该程序的完整代码:

    <?php
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'];
}

//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";
$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();
// -----------------------------------------------------------------------------




/*
$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');
                    }

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');


?>

似乎我一直在尝试做任何可能的事情,以使TCPDF运行得更快,任何建议都将受到赞赏,提前致谢!

1 个答案:

答案 0 :(得分:0)

尽量不使用HTML,使用静态PDF本机方法慢一点,对我们来说,它将pdf加载时间减少到50%,使用:

$pdf->multiCell($a-lot-of-params);

如果您真的关心速度,则显示您的文字......

希望它会有所帮助。