php 缩放 HTML 表格以适应 TCPDF 中的 PDF 页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13621363/
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 05:48:05  来源:igfitidea点击:

Scale an HTML table to fit onto a PDF page in TCPDF

phptcpdf

提问by Raioneru

Is it possible to render a HTML table into PDF using TCPDF and scale it to fit the page? I have many columns in my table. Would it be possible to zoom out or scale it down so it fits?

是否可以使用 TCPDF 将 HTML 表格呈现为 PDF 并对其进行缩放以适合页面?我的表中有很多列。是否可以缩小或缩小它以使其适合?

coloured table cut off by edge of PDF page

被 PDF 页面边缘切断的彩色表格

回答by Baba

TCPDF allows you to write HTML. This way, your table structure does not matter.

TCPDF 允许您编写 HTML。这样,您的表结构无关紧要。

Example:

例子:

$tbl = <<<EOD
<table border="1">
<tr>
<th rowspan="3">Left column</th>
<th colspan="5">Heading Column Span 5</th>
<th colspan="9">Heading Column Span 9</th>
</tr>
<tr>
<th rowspan="2">Rowspan 2<br />This is some text that fills the table cell.</th>
<th colspan="2">span 2</th>
<th colspan="2">span 2</th>
<th rowspan="2">2 rows</th>
<th colspan="8">Colspan 8</th>
</tr>
<tr>
<th>1a</th>
<th>2a</th>
<th>1b</th>
<th>2b</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
<th>7</th>
<th>8</th>
</tr>
</table>
EOD;

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

See Full Full Code| See PDF Output

查看完整完整代码| 查看 PDF 输出

回答by Shankar Damodaran

Send the page dimensions to avoid that. Set your array variable with width and height like this.

发送页面尺寸以避免这种情况。像这样用宽度和高度设置数组变量。

$pageDimension = array('500,300'); //width,height

Substitute this array value where you use this pre-defined TCPDF variable (PDF_PAGE_FORMAT)

在您使用此预定义 TCPPDF 变量 ( PDF_PAGE_FORMAT) 的地方替换此数组值

Replace PDF_PAGE_FORMATwith $pageDimension

替换PDF_PAGE_FORMAT$pageDimension

回答by Thakhani Tharage

You will have to not give your table a custom column width to have the results you want but there will be a lot of wrapping depending on on your content. i think the better way is to choose a different page type like A3 instead of A4 and landscape it.

您不必为您的表格提供自定义列宽以获得您想要的结果,但根据您的内容会有很多换行。我认为更好的方法是选择不同的页面类型,如 A3 而不是 A4 并对其进行横向排列。