php 如何在tcpdf的每一页上放置表格的列标题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14274224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:59:16  来源:igfitidea点击:

How to put column headings of a table on each page in tcpdf?

phptcpdf

提问by PHPLover

The code used to print the pdf is as follows:

用于打印pdf的代码如下:

class MYPDF extends TCPDF {

    // Load table data from file
    public function LoadData($file) {
        // Read file lines
        $lines = file($file);
        $data = array();
        foreach($lines as $line) {
            $data[] = explode(';', chop($line));
        }
        return $data;
    }

    // Colored table
    public function ColoredTable($header,$data) {
        // Colors, line width and bold font
        $this->SetFillColor(233, 136, 64);
        $this->SetTextColor(255);
        $this->SetDrawColor(128, 0, 0);
        $this->SetLineWidth(0.3);
        $this->SetFont('', 'B');
        // Header
        $w = array(10, 40, 20, 20, 20, 20, 20);
        $num_headers = count($header);
        for($i = 0; $i < $num_headers; ++$i) {
            $this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
        }
        $this->Ln();
        // Color and font restoration
        $this->SetFillColor(224, 235, 255);
        $this->SetTextColor(0);
        $this->SetFont('');
        // Data
        $fill = 0;
        foreach($data as $row) {
            $this->Cell($w[0], 6, $row[0], 'LR', 0, 'C', $fill);
            $this->Cell($w[1], 6, $row[1], 'LR', 0, 'L', $fill);
            $this->Cell($w[2], 6, $row[2], 'LR', 0, 'C', $fill);
            $this->Cell($w[3], 6, $row[3], 'LR', 0, 'C', $fill);
            $this->Cell($w[4], 6, $row[4], 'LR', 0, 'C', $fill);
            $this->Cell($w[5], 6, $row[5], 'LR', 0, 'C', $fill);
            $this->Cell($w[6], 6, $row[6], 'LR', 0, 'C', $fill);
            $this->Ln();
            $fill=!$fill;
        }
        $this->Cell(array_sum($w), 0, '', 'T');
    }
}

// create new PDF document
$pdf = new MYPDF(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 011');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set default header data
$pdf->SetHeaderData(PDF_GYANKUTIR_LOGO, PDF_HEADER_LOGO_WIDTH, 'GyanKutir', 
'Jhunjhunu International Wisdom City
Near Khemi Shakti Temple
Jhunjhunu (Rajasthan)-333001');

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$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 strings
$pdf->setLanguageArray($l);

// ---------------------------------------------------------

// set font
$pdf->SetFont('helvetica', '', 6);

// add a page
$pdf->AddPage();

//Column titles
$header = array('Rank', 'Student', 'Time Used', 'Correct Answers', 'Wrong Answers','Questions Skipped','Final Score');


$final_data = $smarty->fetch($file_to_show);

$file = fopen("text.txt","w+");

    file_put_contents($file,"");
    fwrite($file,$final_data);
    fclose($file);

ob_start();
        ob_flush();
//Data loading
$data = $pdf->LoadData('text.txt');

// print colored table
$pdf->ColoredTable($header, $data);

// ---------------------------------------------------------
 ob_flush();
//Close and output PDF document
$pdf->Output('example_011.pdf', 'I');

//============================================================+
// END OF FILE                                                
//============================================================+

I want to add the headings to the table columns on each page. The screenshot is attached with this question. In the first screenshot the headings are getting displayed but in second screen shot the are not getting displayed. How to display those column headings on subsequent pages? Please help me out to resolve this issue. thanks in Advance.This image is the first page of a table in pdf.This is the first page of a table with proper headingsThis is the second page of a table where the headings should be displayed as of first page.

我想将标题添加到每页的表格列中。此问题附有屏幕截图。在第一个屏幕截图中,标题正在显示,但在第二个屏幕截图中没有显示。如何在后续页面上显示这些列标题?请帮我解决这个问题。提前致谢。这张图片是 pdf 表格的第一页。这是具有正确标题的表格的第一页这是表格的第二页,标题应显示为第一页。

采纳答案by EPB

Two methods come to my mind.

我想到了两种方法。

Method 1: Create your own custom header function (like in TCPDF example 3) that automatically adds the header cells to each page. In this scenario, you would move the header cell placement code into a Headermethod in your class and draw the headers to a fixed position on the page. Since the Headermethod is called automatically when a new page is added, your table headers would then be added as soon as the page is created. This is certainly faster and less intensive than method 2, but it can be a little tricky.

方法 1:创建您自己的自定义标题功能(如 TCPDF 示例 3),该功能会自动将标题单元格添加到每个页面。在这种情况下,您会将标题单元格放置代码移动到Header类中的一个方法中,并将标题绘制到页面上的固定位置。由于在Header添加新页面时会自动调用该方法,因此一旦创建页面,您的表标题就会被添加。这当然比方法 2 更快且强度更低,但它可能有点棘手。

Method 2: Use TCPDF transactions to detect when a row is about to break the page, and then rollback. You would then AddPageyourself; draw the headers again; and continue with display. You can do this by getting the number of pages before you make the row, and comparing it to the number of pages after. An example of how you might do it this way as been provided:

方法二:使用TCPDF事务检测某行何时即将断页,然后回滚。然后AddPage你自己;再次绘制标题;并继续显示。您可以通过在创建行之前获取页数并将其与之后的页数进行比较来做到这一点。提供的如何以这种方式执行此操作的示例:

    //Separated Header Drawing into it's own function for reuse.
    public function DrawHeader($header, $w) {
        // Colors, line width and bold font
        // Header
        $this->SetFillColor(233, 136, 64);
        $this->SetTextColor(255);
        $this->SetDrawColor(128, 0, 0);
        $this->SetLineWidth(0.3);
        $this->SetFont('', 'B');        
        $num_headers = count($header);
        for($i = 0; $i < $num_headers; ++$i) {
            $this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
        }
        $this->Ln();
        // Color and font restoration
        $this->SetFillColor(224, 235, 255);
        $this->SetTextColor(0);
        $this->SetFont('');
    }

    // Colored table
    public function ColoredTable($header,$data) {
        $w = array(10, 40, 20, 20, 20, 20, 20);
        $this->DrawHeader($header, $w);

        // Data
        $fill = 0;
        foreach($data as $row) {
            //Get current number of pages.
            $num_pages = $this->getNumPages();
            $this->startTransaction();
            $this->Cell($w[0], 6, $row[0], 'LR', 0, 'C', $fill);
            $this->Cell($w[1], 6, $row[1], 'LR', 0, 'L', $fill);
            $this->Cell($w[2], 6, $row[2], 'LR', 0, 'C', $fill);
            $this->Cell($w[3], 6, $row[3], 'LR', 0, 'C', $fill);
            $this->Cell($w[4], 6, $row[4], 'LR', 0, 'C', $fill);
            $this->Cell($w[5], 6, $row[5], 'LR', 0, 'C', $fill);
            $this->Cell($w[6], 6, $row[6], 'LR', 0, 'C', $fill);
            $this->Ln();
            //If old number of pages is less than the new number of pages,
            //we hit an automatic page break, and need to rollback.
            if($num_pages < $this->getNumPages())
            {
                //Undo adding the row.
                $this->rollbackTransaction(true);
                //Adds a bottom line onto the current page. 
                //Note: May cause page break itself.
                $this->Cell(array_sum($w), 0, '', 'T');
                //Add a new page.
                $this->AddPage();
                //Draw the header.
                $this->DrawHeader($header, $w);
                //Re-do the row.
                $this->Cell($w[0], 6, $row[0], 'LR', 0, 'C', $fill);
                $this->Cell($w[1], 6, $row[1], 'LR', 0, 'L', $fill);
                $this->Cell($w[2], 6, $row[2], 'LR', 0, 'C', $fill);
                $this->Cell($w[3], 6, $row[3], 'LR', 0, 'C', $fill);
                $this->Cell($w[4], 6, $row[4], 'LR', 0, 'C', $fill);
                $this->Cell($w[5], 6, $row[5], 'LR', 0, 'C', $fill);
                $this->Cell($w[6], 6, $row[6], 'LR', 0, 'C', $fill);
                $this->Ln();
            }
            else
            {
                //Otherwise we are fine with this row, discard undo history.
                $this->commitTransaction();
            }
            $fill=!$fill;
        }
        $this->Cell(array_sum($w), 0, '', 'T');
    }

回答by Specto

Another method would be to simply use THEAD tag in html.

另一种方法是在 html 中简单地使用 THEAD 标签。

<table>
<thead>
    <tr><th>Heading</th></tr>
</thead>
<tbody>
    <tr><td>Many rows...</td></tr>
    <tr><td>of data</td></tr>
</tbody>
</table>

It appears TCPDF repeats the heading on every page.

看来 TCPDF 在每一页上都重复标题。

回答by Landrew

This may be a bit late but I came up with a simple way to have headers on each page. Get the count of $dataand use the array_slicefunction to populate a page worth of data rows. Output the table and then loop back to the next page. I use 54 rows per page with a font size of 9.

这可能有点晚了,但我想出了一个简单的方法来在每个页面上都有标题。获取计数$data并使用该array_slice函数填充一页数据行。输出表格,然后循环回到下一页。我每页使用 54 行,字体大小为 9。

//Data loading
$data = $pdf->LoadData('text.txt');
$datacount = count($data);
$i = 0;
while ($i < $datacount) {
    $dataout = array_slice($data, $i, 54, false);
    $pdf->AddPage();
    // print colored table      
    $pdf->ColoredTable($header, $dataout);
    $i = $i + 54;
}

回答by Khrome

EPB's Method 2 worked for me but I modified it a little and just used $pdf->startTransaction(); instead of $this->startTransaction(); as my script was already formated in that way.

EPB 的方法 2 对我有用,但我对其进行了一些修改,只是使用了 $pdf->startTransaction(); 而不是 $this->startTransaction(); 因为我的脚本已经以这种方式格式化了。

Thanks a lot!

非常感谢!