TCPDF:有时添加新页面然后下一页内容溢出 [英] TCPDF: sometime add new page then next page content overflows

查看:36
本文介绍了TCPDF:有时添加新页面然后下一页内容溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

我的输出

推荐答案

我遇到了同样的问题,这个问题的工程解决方案最终是魔术常量和 addPage 用法的组合,如果我的计算表明我们应该手动进行分页:

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.

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

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.

这篇关于TCPDF:有时添加新页面然后下一页内容溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆