Bharat Chauhan
Bharat Chauhan

Reputation: 3322

TCPDF: sometime add new page then next page content overflows

I have to create one pdf with using tcpdf library. I am facing issue regarding content overflow on next page. sometime add new page then next page content overflows. that issue is generated few pages.

MY PHP CODE

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;
}

MY OUTPUT enter image description here

Upvotes: 4

Views: 2016

Answers (2)

oelimoe
oelimoe

Reputation: 360

i had a similair issue but my pdf was made with use of $pdf->writeHTML(). the solution i found was

thead { display: table-header-group }
tfoot { display: table-row-group }
tr { page-break-inside: avoid }

i hope this helps

Upvotes: 0

Jakuje
Jakuje

Reputation: 25926

I was facing the same issue and engineering solution for this problem ended up as a combination of magic constants and usage of addPage if my calculation showed that we should do a manual page break:

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

After writing each row. In your case, you would probably need to adjust the constants, but this solved my problems.

It looks like a bug, that the library is willing to write behind the end of the page region. I don't think, we can get this fixed, because author already moved to development of a new PDF library tc-lib-pdf.

Upvotes: 2

Related Questions