HTML2PDF Table 内容溢出页面
HTML2PDF Table content is overflowing out of page
我正在使用 HTML2PDF 库,但在尝试输出 pdf 时遇到问题。实际上,我的table总是被截断,不被截断就无法显示我的table的全部内容。
我定义了 css 样式,但似乎必须 effect.Moreover,如果我没有在 echo 语句 table border="1px" 上定义到我的 php 脚本中,它就不会t 采用先前定义的样式 css。
这是我的代码:
<!DOCTYPE html>
<html>
<head>
<title>Test PDF</test>
<style>
table{width:100%; border-collapse:collapse; table-layout:auto; vertical-align:top; margin-bottom:15px; border:1px solid #CCCCCC;}
table thead th{font-size: 2px; color:#FFFFFF; background-color:#666666; border:1px solid #CCCCCC; border-collapse:collapse; text-align:center; table-layout:auto; vertical-align:middle;}
table tbody td{vertical-align:top; border-collapse:collapse; border-left:1px solid #CCCCCC; border-right:1px solid #CCCCCC; font-size: 2px;}
table thead th, table tbody td{padding:5px; border-collapse:collapse;}
table tbody tr.light{color:#979797; background-color:#F7F7F7;}
table tbody tr.dark{color:#979797; background-color:#E8E8E8;}
</style>
</head>
<body>
<html>
<?php
require_once "config.php";
//Start html2pdpf session
ob_start();
//Get tablename
$tablename=$_POST["tablename"];
$ShowPivotTableResult='SELECT * FROM '.$tablename.'';
$resultLeast=mysqli_query($conn,$ShowPivotTableResult) or die(mysqli_error($conn));
if (!$resultLeast) {
die("Query to show fields from table failed");
}
//Display Pivot Table content - script
$fields_num = mysqli_num_fields($resultLeast);
echo "<table border='1px' CELLSPACING='0' cellpadding='2'><thead><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysqli_fetch_fields($resultLeast);
echo '<th>'.$field[$i]->name.'</th>';
}
echo "</tr></thead><tbody>\n";
// printing table rows
while($rowLEAST = mysqli_fetch_row($resultLeast))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($rowLEAST as $cellPIVOT)
echo "<td>$cellPIVOT</td>";
echo "</tr>\n";
}
echo '</tbody></table><br/><br/>';
$content = ob_get_clean();
// convert
require_once('html2pdf.class.php');
try
{
$html2pdf = new HTML2PDF('L', 'A4', 'fr', true, 'UTF-8', 0);
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->writeHTML($content, isset($_GET['vuehtml']));
ob_end_clean();
$html2pdf->Output(''.$tablename.'.pdf');
}
catch(HTML2PDF_exception $e) {
echo $e;
exit;
}
mysqli_close($conn);
?>
</body>
</html>
尝试在 table 宽度上使用绝对长度,例如 pt。 A4 的宽度为 595pt。
关于 css 属性,尽量使用 class 而不是标签。
示例:
HTML
<style>
.tbl{
width: 595pt;
border: 1px solid #000;
}
</style>
PHP
echo "<table class='tbl' CELLSPACING='0' cellpadding='2'><thead><tr>";
如果您以百分比指定 table 单元格宽度,请尝试将其删除并使用样式属性控制宽度。宽度应以毫米为单位,与页面宽度成正比。
例如如果您的页面是 A4 - 纵向宽度为 595.276。 table 单元格的 50% 宽度将采用样式属性中的 (595.276/2)-边距宽度。
我正在使用 HTML2PDF 库,但在尝试输出 pdf 时遇到问题。实际上,我的table总是被截断,不被截断就无法显示我的table的全部内容。 我定义了 css 样式,但似乎必须 effect.Moreover,如果我没有在 echo 语句 table border="1px" 上定义到我的 php 脚本中,它就不会t 采用先前定义的样式 css。
这是我的代码:
<!DOCTYPE html>
<html>
<head>
<title>Test PDF</test>
<style>
table{width:100%; border-collapse:collapse; table-layout:auto; vertical-align:top; margin-bottom:15px; border:1px solid #CCCCCC;}
table thead th{font-size: 2px; color:#FFFFFF; background-color:#666666; border:1px solid #CCCCCC; border-collapse:collapse; text-align:center; table-layout:auto; vertical-align:middle;}
table tbody td{vertical-align:top; border-collapse:collapse; border-left:1px solid #CCCCCC; border-right:1px solid #CCCCCC; font-size: 2px;}
table thead th, table tbody td{padding:5px; border-collapse:collapse;}
table tbody tr.light{color:#979797; background-color:#F7F7F7;}
table tbody tr.dark{color:#979797; background-color:#E8E8E8;}
</style>
</head>
<body>
<html>
<?php
require_once "config.php";
//Start html2pdpf session
ob_start();
//Get tablename
$tablename=$_POST["tablename"];
$ShowPivotTableResult='SELECT * FROM '.$tablename.'';
$resultLeast=mysqli_query($conn,$ShowPivotTableResult) or die(mysqli_error($conn));
if (!$resultLeast) {
die("Query to show fields from table failed");
}
//Display Pivot Table content - script
$fields_num = mysqli_num_fields($resultLeast);
echo "<table border='1px' CELLSPACING='0' cellpadding='2'><thead><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysqli_fetch_fields($resultLeast);
echo '<th>'.$field[$i]->name.'</th>';
}
echo "</tr></thead><tbody>\n";
// printing table rows
while($rowLEAST = mysqli_fetch_row($resultLeast))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($rowLEAST as $cellPIVOT)
echo "<td>$cellPIVOT</td>";
echo "</tr>\n";
}
echo '</tbody></table><br/><br/>';
$content = ob_get_clean();
// convert
require_once('html2pdf.class.php');
try
{
$html2pdf = new HTML2PDF('L', 'A4', 'fr', true, 'UTF-8', 0);
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->writeHTML($content, isset($_GET['vuehtml']));
ob_end_clean();
$html2pdf->Output(''.$tablename.'.pdf');
}
catch(HTML2PDF_exception $e) {
echo $e;
exit;
}
mysqli_close($conn);
?>
</body>
</html>
尝试在 table 宽度上使用绝对长度,例如 pt。 A4 的宽度为 595pt。 关于 css 属性,尽量使用 class 而不是标签。 示例:
HTML
<style>
.tbl{
width: 595pt;
border: 1px solid #000;
}
</style>
PHP
echo "<table class='tbl' CELLSPACING='0' cellpadding='2'><thead><tr>";
如果您以百分比指定 table 单元格宽度,请尝试将其删除并使用样式属性控制宽度。宽度应以毫米为单位,与页面宽度成正比。
例如如果您的页面是 A4 - 纵向宽度为 595.276。 table 单元格的 50% 宽度将采用样式属性中的 (595.276/2)-边距宽度。