我正在创建一个大型 HTML 表格,但我遇到了分页符问题,如下图所示:
是否有自动解决问题的方法?或者有什么方法可以做到?
8 回答
尝试将此添加到您的<tr>
标签中:nobr="true"
.
所以一个简单的例子是:
<table>
<tr nobr="true">
<td>Test</td>
<td>Test 2</td>
</tr>
</table>
这nobr="true"
可以防止表格行分开。你也可以把它放在标签<td>
上。<th>
我知道这是一个相当老的问题,但我今天遇到了同样的问题,我的表格在页面之间拆分,我从 FastTrack 的答案中进一步调查了该方法,结果发现您也可以将nobr="true"
属性用于<table>
标签。也就是说,对我来说,这样的代码解决了这个问题:
<table nobr="true">
<tr>
<td>Test</td>
<td>Test 2</td>
</tr>
</table>
警告 - 此代码仅在您的表格小于一页时才有意义。
我对重叠的标题有同样的问题。我尝试了 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());
}
}
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
}
}
在你的代码中
// set top margin to style pages 2, 3..
$pdf->SetMargins(15, $pdf->top_margin, 15);
奇怪的是,这里提到的解决方案对我不起作用。好吧,它确实有点,但标签内的内容会重复(根据需要),但如果它是行跨单元格,则会导致上方或下方单元格的布局问题。当我进行实验时,它变得更糟了。
我的解决方案虽然不优雅,但将 AutoPageBreak 设置为 false,放置一个行增量计数器,为每一行递增,然后检查它是否超过了某个值。如果是这样,我关闭表,使用 writeHTML(),调用 addPage(),然后继续,将其重建为新表、标题和所有内容。
就像我说的,不优雅,但它有效。我希望这对某人有所帮助......这是一个相当明显的解决方案,但执行并不总是那么明显。此外,可能有更好的方法适用于您的特定情况,但如果没有,请尝试我的方法。:)
对于感兴趣的人,只需执行以下操作,它就会像魅力一样工作:
$pdf->SetMargins(0, 0, 0);
$pdf->SetHeaderMargin(0);
$pdf->SetFooterMargin(0);
一些 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
//============================================================+
你有没有尝试过
<table>
<thead>
<tr>
<td>
This is my header which appears on every page
</td>
</tr>
</thead>
<tr>
<td>
My Content
</td>
</tr>
</table>
我正在使用 smarty,这样您就有更多的可能性手动打破表格(例如,如果您使用边框)。需要的话我也发这个。。。