TCPDF:有时添加新页面然后下一页内容溢出

时间:2016-02-10 14:45:42

标签: php tcpdf

我必须使用tcpdf库创建一个pdf。我在下一页面临关于内容溢出的问题。有时添加新页面然后下一页内容溢出。该问题产生了几页。

我的PHP代码

public function getPdfToAllEmpReport(){

    //header("Content-type:application/pdf");
    $user_id = Auth::user()->get()->id;
    $company_id = Auth::user()->get()->company_id;
    $input = Input::all();
    if(!isset($input) && empty($input)){
        $input = array();
    }

    // Company based Userlist
    $userData = CompanyUser::getCompanyUserList()->toArray();
    $userRewardsData = $this->getUsersRewards($userData,$input);

    $company = CompanyMaster::getCompanyDetails()->toArray();
    $search_string = "";
    if(!empty($company) && !empty($userRewardsData)){

        PDF::SetTitle($company['company_name']." - All Employee Report");

        PDF::SetHeaderMargin(5);
        PDF::SetFooterMargin(10);

        PDF::SetFont('','',10);
        PDF::SetHeaderData('', '', $company['company_name']." - All Employee Report", $search_string);
        PDF::SetMargins(10, 20, 10);

        PDF::AddPage();

        $w = array(50,20,20,20,19,19,19,23);
        $sw = array(60,56,27,27,20);
        $h = 5;

        $total_height = 0;
        PDF::Ln();
        PDF::SetFillColor(128, 0, 96);
        PDF::SetTextColor(255, 255, 255);
        PDF::SetFont('','B',9);
        // Data
        PDF::MultiCell($w[0], 10, "Worker Name", 'LRB', 'L' , 1, 0);
        PDF::MultiCell($w[1], 10, "Applause Received", 'RB', 'L', 1, 0);
        PDF::MultiCell($w[2], 10, "Drumbeat Received", 'RB', 'L' , 1, 0);
        PDF::MultiCell($w[3], 10, "Ding Received", 'RB', 'L' , 1, 0);
        PDF::MultiCell($w[4], 10, "Shoutout Sent", 'RB', 'L' , 1, 0);
        PDF::MultiCell($w[5], 10, "Rally Response", 'RB', 'L' , 1, 0);
        PDF::MultiCell($w[6], 10, "Project Reward", 'RB', 'L' , 1, 0);
        PDF::MultiCell($w[7], 10, "Engagement Donation", 'RB', 'L' , 1, 0);
        PDF::Ln();
        $i=1;

        $total_height = 10;
        foreach ($userRewardsData['finalData'] as $user) {
            PDF::SetFillColor(38, 115, 38);

            PDF::SetTextColor(255,255,255);
            PDF::SetFont('','B',9);
            PDF::MultiCell($w[0], $h, $user['user_info']['full_name'], 'LRB', 'L', 1, 0);
            PDF::MultiCell($w[1], $h, number_format($user['applauseData']['count']), 'RB', 'L', 1, 0);
            PDF::MultiCell($w[2], $h, number_format($user['dingData']['count']), 'RB', 'L', 1, 0);
            PDF::MultiCell($w[3], $h, number_format($user['drumbeatData']['count']), 'RB', 'L', 1, 0);
            PDF::MultiCell($w[4], $h, number_format($user['shoutoutData']['count']), 'RB', 'L', 1, 0);
            PDF::MultiCell($w[5], $h, number_format($user['rallyResponceData']['count']), 'RB', 'L', 1, 0);
            PDF::MultiCell($w[6], $h, number_format($user['projectData']['count']), 'RB', 'L', 1, 0);
            PDF::MultiCell($w[7], $h, number_format($user['engagementData']['count']), 'RB', 'L', 1, 0);
            PDF::Ln();
            PDF::SetFont('','',9);
            PDF::SetTextColor(0,0,0);
            $total_height = $this->setNewPage($total_height,$h,$search_string);
            if(!empty($user['applauseData']['data'])){
                PDF::SetFillColor(180, 180, 180);
                PDF::MultiCell($sw[0], $h, "Applause - Reward Name", 'LRB', 'L', 1, 0);
                PDF::MultiCell($sw[1], $h, "Issued by", 'RB', 'L', 1, 0);
                PDF::MultiCell($sw[2], $h, "Active / Inactive", 'RB', 'L', 1, 0);
                PDF::MultiCell($sw[3], $h, "Issue date", 'RB', 'L', 1, 0);
                PDF::MultiCell($sw[4], $h, "Points", 'RB', 'L', 1, 0);
                PDF::Ln();

                $total_height = $this->setNewPage($total_height,$h,$search_string);
                foreach ($user['applauseData']['data'] as $data) {
                    PDF::SetFillColor(255, 255, 255);
                    PDF::MultiCell($sw[0], $h, $data['reward_name'], 'LRB', 'L', 1, 0);
                    PDF::MultiCell($sw[1], $h, $data['from_user'], 'RB', 'L', 1, 0);
                    PDF::MultiCell($sw[2], $h, "YES", 'RB', 'L', 1, 0);
                    PDF::MultiCell($sw[3], $h, $data['attaboy_date'], 'RB', 'L', 1, 0);
                    PDF::MultiCell($sw[4], $h, number_format($data['points']), 'RB', 'L', 1, 0);
                    PDF::Ln();
                    $total_height = $this->setNewPage($total_height,$h,$search_string);
                }
            }

            if(!empty($user['dingData']['data'])){
                PDF::SetFillColor(180, 180, 180);
                PDF::MultiCell($sw[0], $h, "Ding - Reward Name", 'LRB', 'L', 1, 0);
                PDF::MultiCell($sw[1], $h, "Issued by", 'RB', 'L', 1, 0);
                PDF::MultiCell($sw[2], $h, "Active / Inactive", 'RB', 'L', 1, 0);
                PDF::MultiCell($sw[3], $h, "Issue date", 'RB', 'L', 1, 0);
                PDF::MultiCell($sw[4], $h, "Points", 'RB', 'L', 1, 0);
                PDF::Ln();
                $total_height = $this->setNewPage($total_height,$h,$search_string);
                foreach ($user['dingData']['data'] as $data) {
                    PDF::SetFillColor(255, 255, 255);
                    PDF::MultiCell($sw[0], $h, $data['reward_name'], 'LRB', 'L', 1, 0);
                    PDF::MultiCell($sw[1], $h, $data['from_user'], 'RB', 'L', 1, 0);
                    PDF::MultiCell($sw[2], $h, "YES", 'RB', 'L', 1, 0);
                    PDF::MultiCell($sw[3], $h, $data['attaboy_date'], 'RB', 'L', 1, 0);
                    PDF::MultiCell($sw[4], $h, number_format($data['points']), 'RB', 'L', 1, 0);
                    PDF::Ln();
                    $total_height = $this->setNewPage($total_height,$h,$search_string);
                }
            }

            if(!empty($user['drumbeatData']['data'])){
                PDF::SetFillColor(180, 180, 180);
                PDF::MultiCell($sw[0], $h, "Drumbeat - Reward Name", 'LRB', 'L', 1, 0);
                PDF::MultiCell($sw[1], $h, "Issued by", 'RB', 'L', 1, 0);
                PDF::MultiCell($sw[2], $h, "Active / Inactive", 'RB', 'L', 1, 0);
                PDF::MultiCell($sw[3], $h, "Issue date", 'RB', 'L', 1, 0);
                PDF::MultiCell($sw[4], $h, "Points", 'RB', 'L', 1, 0);
                PDF::Ln();
                $total_height = $this->setNewPage($total_height,$h,$search_string);
                foreach ($user['drumbeatData']['data'] as $data) {
                    PDF::SetFillColor(255, 255, 255);
                    PDF::MultiCell($sw[0], $h, $data['reward_name'], 'LRB', 'L', 1, 0);
                    PDF::MultiCell($sw[1], $h, $data['from_user'], 'RB', 'L', 1, 0);
                    PDF::MultiCell($sw[2], $h, "YES", 'RB', 'L', 1, 0);
                    PDF::MultiCell($sw[3], $h, $data['attaboy_date'], 'RB', 'L', 1, 0);
                    PDF::MultiCell($sw[4], $h, number_format($data['points']), 'RB', 'L', 1, 0);
                    PDF::Ln();
                    $total_height = $this->setNewPage($total_height,$h,$search_string);
                }
            }

            if(!empty($user['shoutoutData']['data'])){
                PDF::SetFillColor(180, 180, 180);
                PDF::MultiCell($sw[0], $h, "Shoutout - Reward Name", 'LRB', 'L', 1, 0);
                PDF::MultiCell($sw[1], $h, "Issued by", 'RB', 'L', 1, 0);
                PDF::MultiCell($sw[2], $h, "Active / Inactive", 'RB', 'L', 1, 0);
                PDF::MultiCell($sw[3], $h, "Issue date", 'RB', 'L', 1, 0);
                PDF::MultiCell($sw[4], $h, "Points", 'RB', 'L', 1, 0);
                PDF::Ln();
                $total_height = $total_height+$h;
                if($total_height%260==0){
                    PDF::AddPage();
                    $total_height =0;
                }
                foreach ($user['shoutoutData']['data'] as $data) {
                    PDF::SetFillColor(255, 255, 255);
                    PDF::MultiCell($sw[0], $h, $data['reward_name'], 'LRB', 'L', 1, 0);
                    PDF::MultiCell($sw[1], $h, $data['from_user'], 'RB', 'L', 1, 0);
                    PDF::MultiCell($sw[2], $h, "YES", 'RB', 'L', 1, 0);
                    PDF::MultiCell($sw[3], $h, $data['attaboy_date'], 'RB', 'L', 1, 0);
                    PDF::MultiCell($sw[4], $h, number_format($data['points']), 'RB', 'L', 1, 0);
                    PDF::Ln();
                    $total_height = $this->setNewPage($total_height,$h,$search_string);
                }
            }

            if(!empty($user['rallyResponceData']['data'])){
                PDF::SetFillColor(180, 180, 180);
                PDF::MultiCell($sw[0], $h, "Rally Responce - Reward Name", 'LRB', 'L', 1, 0);
                PDF::MultiCell($sw[1], $h, "Issued by", 'RB', 'L', 1, 0);
                PDF::MultiCell($sw[2], $h, "Active / Inactive", 'RB', 'L', 1, 0);
                PDF::MultiCell($sw[3], $h, "Issue date", 'RB', 'L', 1, 0);
                PDF::MultiCell($sw[4], $h, "Points", 'RB', 'L', 1, 0);
                PDF::Ln();
                $total_height = $this->setNewPage($total_height,$h,$search_string);
                foreach ($user['rallyResponceData']['data'] as $data) {
                    PDF::SetFillColor(255, 255, 255);
                    PDF::MultiCell($sw[0], $h, $data['reward_name'], 'LRB', 'L', 1, 0);
                    PDF::MultiCell($sw[1], $h, $data['from_user'], 'RB', 'L', 1, 0);
                    PDF::MultiCell($sw[2], $h, "YES", 'RB', 'L', 1, 0);
                    PDF::MultiCell($sw[3], $h, $data['attaboy_date'], 'RB', 'L', 1, 0);
                    PDF::MultiCell($sw[4], $h, number_format($data['points']), 'RB', 'L', 1, 0);
                    PDF::Ln();
                    $total_height = $this->setNewPage($total_height,$h,$search_string);
                }
            }

            if(!empty($user['projectData']['data'])){
                PDF::SetFillColor(180, 180, 180);
                PDF::MultiCell($sw[0], $h, "Project - Reward Name", 'LRB', 'L', 1, 0);
                PDF::MultiCell($sw[1], $h, "Issued by", 'RB', 'L', 1, 0);
                PDF::MultiCell($sw[2], $h, "Active / Inactive", 'RB', 'L', 1, 0);
                PDF::MultiCell($sw[3], $h, "Issue date", 'RB', 'L', 1, 0);
                PDF::MultiCell($sw[4], $h, "Points", 'RB', 'L', 1, 0);
                PDF::Ln();
                $total_height = $this->setNewPage($total_height,$h,$search_string);
                foreach ($user['projectData']['data'] as $data) {
                    PDF::SetFillColor(255, 255, 255);
                    PDF::MultiCell($sw[0], $h, $data['reward_name'], 'LRB', 'L', 1, 0);
                    PDF::MultiCell($sw[1], $h, $data['from_user'], 'RB', 'L', 1, 0);
                    PDF::MultiCell($sw[2], $h, "YES", 'RB', 'L', 1, 0);
                    PDF::MultiCell($sw[3], $h, $data['display_issued_date'], 'RB', 'L', 1, 0);
                    PDF::MultiCell($sw[4], $h, number_format($data['points']), 'RB', 'L', 1, 0);
                    PDF::Ln();
                    $total_height = $this->setNewPage($total_height,$h,$search_string);
                }
            }

            if(!empty($user['engagementData']['data'])){
                PDF::SetFillColor(180, 180, 180);
                PDF::MultiCell($sw[0], $h, "Engagement - Reward Name", 'LRB', 'L', 1, 0);
                PDF::MultiCell($sw[1], $h, "Issued by", 'RB', 'L', 1, 0);
                PDF::MultiCell($sw[2], $h, "Active / Inactive", 'RB', 'L', 1, 0);
                PDF::MultiCell($sw[3], $h, "Issue date", 'RB', 'L', 1, 0);
                PDF::MultiCell($sw[4], $h, "Points", 'RB', 'L', 1, 0);
                PDF::Ln();
                $total_height = $this->setNewPage($total_height,$h,$search_string);
                foreach ($user['engagementData']['data'] as $data) {
                    PDF::SetFillColor(255, 255, 255);
                    PDF::MultiCell($sw[0], $h, $data['reward_name'], 'LRB', 'L', 1, 0);
                    PDF::MultiCell($sw[1], $h, $data['from_user'], 'RB', 'L', 1, 0);
                    PDF::MultiCell($sw[2], $h, "YES", 'RB', 'L', 1, 0);
                    PDF::MultiCell($sw[3], $h, $data['display_issued_date'], 'RB', 'L', 1, 0);
                    PDF::MultiCell($sw[4], $h, number_format($data['points']), 'RB', 'L', 1, 0);
                    PDF::Ln();
                    $total_height = $this->setNewPage($total_height,$h,$search_string);
                }
            }                
            $i++;

            $total_height = $this->setNewPage($total_height,$h,$search_string);
        }
        PDF::Output('hello_world.pdf','I');
    }
}

private function setNewPage($total_height,$height,$search_string){
    $total_height = $total_height+$height;
    if($total_height%275==0){
        PDF::AddPage();
        $total_height =10;
    }
    return $total_height;
}

我的输出 enter image description here

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,这个问题的工程解决方案最终结合了魔术常量和addPage的使用,如果我的计算显示我们应该手动分页:

$top_margin = 82;
[...]
if ($pdf->getY() > (300 /* A4 height */ - $top_margin + 40 /* another magic constant */)) {
    $pdf->addPage();
}

写完每一行后。在你的情况下,你可能需要调整常量,但这解决了我的问题。

看起来像一个错误,该库愿意在页面区域的末尾写入。我不认为,我们可以解决这个问题,因为作者已经开始开发新的PDF库tc-lib-pdf