tcpdf html table,TCPDF: HTML table and page breaks

I am creating a large HTML table and I have problem with page breaks as you can see in the following image:

b1c564065c8f886646ea5ab0f72156f4.png

Is there a method settle down the problem automatically? Or what is the way to do it?

Try adding this to your

tags: nobr="true".

So a quick example would be:

TestTest 2

The nobr="true" prevents the table rows from ever breaking apart. You can also put this on

and tags.

I know it's rather old question, but I had this same issue today, my table was splitted between pages and I investigated a little bit further on the method from FastTrack's answer and it turned out that you can also use the nobr="true" attribute also to the

TestTest 2

Warning - this code makes sense only when your tables are smaller than one page.

I had the same problem with overlapping headers.

I tried yevgeny solution, but that required some more editions to my PDF generator code (I have lots of PDFs outputs written in FPDF and I wanted to minimize the process of miograting them to TCPDF), so I used this more simple solution

class MYPDF extenfs TCPDF {

//your initialization code

function header(){

//your code here

//we change only the top margin and this executes for every header in every page, even the frst one

$this->SetTopMargin($this->GetY());

}

}

yevgeny

roney, thank you so much, writing HTML generated overlaps with the header for pages 2,3 .. this worked for me:

class your_PDF extends TCPDF

{

var $top_margin = 20;

function Header() {

// set top margin to style pages 2, 3..

//title goes here

$this->top_margin = $this->GetY() + 5; // padding for second page

}

}

in your code

// set top margin to style pages 2, 3..

$pdf->SetMargins(15, $pdf->top_margin, 15);

For the interested, just do as follows and it will work like a charm:

$pdf->SetMargins(0, 0, 0);

$pdf->SetHeaderMargin(0);

$pdf->SetFooterMargin(0);

Strangely enough the solutions mentioned here didn't work for me. Well, it did sortof, but content inside of tags would get repeated (as desired) but would then cause layout issues for the cell above or below if it was rowspanned. As I experimented, it just got worse.

My solution, while inelegant, was to set AutoPageBreak to false, put a row incrementer counter up, incrementing for each row, then checked if it had exceeded a certain value. If so, I closed the table, used writeHTML(), called addPage() and then continued , having rebuilt it as a new table, headers and all.

Like I said, inelegant, but it worked. I hope this helps someone ... it's a fairly obvious solution but execution is not always quite so obvious. Also, there may be a better way that works for your particular situation, but if it doesn't, give mine a try. :)

Some CSS solved for me:

// Include the main TCPDF library (search for installation path).

require_once('tcpdf/tcpdf.php');

// create new PDF document

$pdf = new TCPDF('R', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information

$pdf->SetCreator(PDF_CREATOR);

$pdf->SetAuthor('Author');

$pdf->SetTitle('TCPDF HTML Table');

$pdf->SetSubject('TCPDF Tutorial');

$pdf->SetKeywords('TCPDF, PDF, html,table, example, test, guide');

// set default header data

$pdf->SetHeaderData('', '', ' HTML table', '');

// 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(15, 15, 15);

$pdf->SetHeaderMargin(15);

$pdf->SetFooterMargin(15);

// set auto page breaks

$pdf->SetAutoPageBreak(TRUE, 15);

// set image scale factor

//$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

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

// set font

$pdf->SetFont('times', '', 10);

// add a page

$pdf->AddPage();

$start = 1;

$end = 254;

$step = 1;

$arr = range($start, $end, $step);

$table_header .= sprintf("

%s%s%s%s", 'IP', 'Computer', 'User', 'Fone');

foreach ($arr as $ar) {

$row[] = $ar;

}

foreach ($row as $r):

if (($r % 40) === 0):

$table_header;

endif;

$table .= sprintf("

\n%s\n%s\n%s\n%s\n\n", $r, $r, $r, $r);

endforeach;

$now = date("d/m/Y");

$caption = "

IP addresses $now\n";

$n = "\n";

$tbl = <<

table{

font-family: serif;

font-size: 11pt;

}

table tr {

}

table tr td {

padding:3px;

border:#000000 solid 1px;

}

em {

font-size: 4pt;

}

tr { white-space:nowrap; }

{$caption}

{$table_begin}

{$table_header}

{$table}

EOD;

$pdf->writeHTML($tbl, true, false, false, false, '');

// reset pointer to the last page

//$pdf->lastPage();

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

//Close and output PDF document

$pdf->Output('html_table.pdf', 'I');

//============================================================+

// END OF FILE

//============================================================+

have you tried

This is my header which appears on every page

My Content

I'm using smarty, with this you have more possibilities to manually break the table (e.g. if you're using borders). If needed, i'll post this, too...

来源:https://stackoverflow.com/questions/5846164/tcpdf-html-table-and-page-breaks

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值