2016-03-04 75 views
2

我正在使用HTML2PDF庫,當我試圖輸出我的PDF時遇到問題。實際上,我的桌子總是被截斷,無法顯示我的桌子的全部內容而沒有裁剪。 我定義了css風格,但它似乎不得不影響。另外,如果我沒有在echo語句表border =「1px」中定義到我的php腳本中,它不會採用先前定義的樣式css。HTML2PDF表格內容溢出頁面

這裏我的代碼:

<!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> 

回答

3

嘗試使用像PT表格的寬度絕對長度。 A4有595pt寬度。 關於CSS屬性,請嘗試使用類而不是標籤。 例如:

HTML

<style> 
    .tbl{ 
    width: 595pt; 
    border: 1px solid #000; 
    } 
</style> 

PHP

echo "<table class='tbl' CELLSPACING='0' cellpadding='2'><thead><tr>"; 
+0

@rmondesilva您好,感謝您的幫助。您的解決方案適用於表格邊框。但是我的A4表格仍然被A4截斷。我嘗試在風景模式和'.tbl {width:842pt;}'中使用'html2pdf = new HTML2PDF('L','A4','fr',true,'UTF-8',0);'我的桌面內容溢出頁面 – user979974

+1

不客氣,我很高興它的工作原理。在另一個問題中,你是否嘗試在表格單元格/頭文件上使用CSS屬性'white-space:nowrap;'?並嘗試使用「自動換行:分詞」或「分詞:分離;」。在表格中放置一個'table-layout:fixed;'併爲表格標題/單元格設置一個修正/絕對長度。您的問題的可能原因是因爲您的內容,所以請嘗試這些解決方案。 – rmondesilva