【发布时间】:2011-08-16 07:30:44
【问题描述】:
【问题讨论】:
-
到底是什么问题?一个单元格分为 2 页?
-
在下面查看我的答案。它应该可以帮助那些仍在为这个问题苦苦挣扎的人。
-
这个问题的正确答案应该会有所帮助:manual-page-break-in-tcpdf
【问题讨论】:
我知道这是一个相当老的问题,但我今天也遇到了同样的问题,我的表格被分成几页,我从 FastTrack 的回答中进一步调查了方法,结果发现您也可以使用 nobr="true"也属于<table> 标签。也就是说,对我来说,这样的代码解决了这个问题:
<table nobr="true">
<tr>
<td>Test</td>
<td>Test 2</td>
</tr>
</table>
警告 - 此代码仅在您的表格小于一页时才有意义。
【讨论】:
为我解决了一些 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
//============================================================+
【讨论】:
我在重叠标题时遇到了同样的问题。 我尝试了 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());
}
}
【讨论】:
尝试将此添加到您的 <tr> 标签:nobr="true"。
所以一个简单的例子是:
<table>
<tr nobr="true">
<td>Test</td>
<td>Test 2</td>
</tr>
</table>
nobr="true" 可防止表格行分开。你也可以把它放在<td>和<th>标签上。
【讨论】:
你试过了吗
<table>
<thead>
<tr>
<td>
This is my header which appears on every page
</td>
</tr>
</thead>
<tr>
<td>
My Content
</td>
</tr>
</table>
我正在使用 smarty,这样你就有更多的机会手动打破表格(例如,如果你使用边框)。如果需要,我也会发布这个......
【讨论】:
奇怪的是,这里提到的解决方案对我不起作用。好吧,它确实有点,但是标签内的内容会重复(根据需要),但如果它是行跨度的,则会导致上方或下方的单元格的布局问题。经过我的实验,情况变得更糟了。
我的解决方案虽然不优雅,但将 AutoPageBreak 设置为 false,设置一个行增量计数器,为每一行递增,然后检查它是否超过了某个值。如果是这样,我关闭表格,使用 writeHTML(),调用 addPage(),然后继续,将其重建为新表格、标题和所有内容。
就像我说的那样,不优雅,但它确实有效。我希望这对某人有所帮助......这是一个相当明显的解决方案,但执行并不总是那么明显。此外,可能有更好的方法适用于您的特定情况,但如果没有,请尝试我的方法。 :)
【讨论】:
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);
【讨论】:
有兴趣的可以按照以下方式进行操作,效果如下:
$pdf->SetMargins(0, 0, 0);
$pdf->SetHeaderMargin(0);
$pdf->SetFooterMargin(0);
【讨论】: