php TCPDF:HTML 表格和分页符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5846164/
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
TCPDF: HTML table and page breaks
提问by Koney
回答by FastTrack
Try adding this to your <tr>
tags: nobr="true"
.
尝试将其添加到您的<tr>
标签中:nobr="true"
。
So a quick example would be:
所以一个简单的例子是:
<table>
<tr nobr="true">
<td>Test</td>
<td>Test 2</td>
</tr>
</table>
The nobr="true"
prevents the table rows from ever breaking apart. You can also put this on <td>
and <th>
tags.
这nobr="true"
可以防止表格行分崩离析。你也可以把它放在<td>
和<th>
标签上。
回答by Artur Karczmarczyk
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 <table>
tag. That is, for me such code solved this problem:
我知道这是一个相当老的问题,但我今天遇到了同样的问题,我的表格被分成了几页,我进一步调查了 FastTrack 的答案中的方法,结果发现您也可以将该nobr="true"
属性用于<table>
标签。也就是说,对我来说,这样的代码解决了这个问题:
<table nobr="true">
<tr>
<td>Test</td>
<td>Test 2</td>
</tr>
</table>
Warning - this code makes sense only when your tables are smaller than one page.
警告 - 只有当您的表格小于一页时,此代码才有意义。
回答by Federico Giraldi
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
我在重叠标题方面遇到了同样的问题。我尝试了 yevgeny 解决方案,但这需要更多版本的 PDF 生成器代码(我有很多用 FPDF 编写的 PDF 输出,我想尽量减少将它们迁移到 TCPDF 的过程),所以我使用了这个更简单的解决方案
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());
}
}
回答by yevgeny
roney, thank you so much, writing HTML generated overlaps with the header for pages 2,3 .. this worked for me:
roney,非常感谢,编写 HTML 生成与页面 2,3 的标题重叠.. 这对我有用:
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);
回答by David
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.
我的解决方案虽然不优雅,但将 AutoPageBreak 设置为 false,将行增量计数器向上,每行递增,然后检查它是否超过某个值。如果是这样,我关闭表格,使用 writeHTML(),调用 addPage() 然后继续,将其重建为新表格、标题和所有内容。
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. :)
就像我说的,不优雅,但它奏效了。我希望这对某人有所帮助......这是一个相当明显的解决方案,但执行并不总是那么明显。此外,可能有更好的方法适用于您的特定情况,但如果没有,请尝试我的方法。:)
回答by roney
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);
回答by msmafra
Some CSS solved for me:
一些 CSS 为我解决了:
// 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("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>", 'IP', 'Computer', 'User', 'Fone');
foreach ($arr as $ar) {
$row[] = $ar;
}
foreach ($row as $r):
if (($r % 40) === 0):
$table_header;
endif;
$table .= sprintf("<tr>\n<td>%s</td>\n<td>%s</td>\n<td>%s</td>\n<td>%s</td>\n</tr>\n", $r, $r, $r, $r);
endforeach;
$now = date("d/m/Y");
$caption = "<caption>IP addresses <em>$now</em></caption>\n";
$n = "\n";
$tbl = <<<EOD
<style>
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; }
</style>
<h1>{$caption}</h1>
{$table_begin}
{$table_header}
{$table}
</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
//============================================================+
回答by manuxi
have you tried
你有没有尝试过
<table>
<thead>
<tr>
<td>
This is my header which appears on every page
</td>
</tr>
</thead>
<tr>
<td>
My Content
</td>
</tr>
</table>
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...
我正在使用 smarty,这样您就有更多可能手动打破表格(例如,如果您使用边框)。如果需要,我也会发布这个......