2012-05-23 63 views
0

我需要從Php(html Table)轉換爲Pdf。我已經使用DOM PDf進行轉換。但我沒有得到pdf的正確觀點。請找到下面的圖片。從PHP和PDF轉換爲

PDF查看

PdfView

HTML查看

Html View

下面的代碼:

<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
    </head> 
<?php if(count($this->userDetails)>=1){ for($a=0;$a<count($this->userDetails);$a++){ $user = $this->userDetails[$a]; ?> 
    <body> 
    <div style="background-image: url(http://192.168.2.26/skins/schooladmin/images/id_bg.png); background-repeat: repeat; width:400px; height:220px;margin:10px 0px 0px 10px;float:left;position:relative;"> 
<table style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:14px; font-weight:bold; color:#fff; background-image:url(http://192.168.2.26/skins/schooladmin/images/id_white_bg.png); background-repeat:no-repeat;" width="100%" border="0" cellpadding="5" cellspacing="5"> 
    <tr> 
    <td style="background-image: url(http://192.168.2.26/skins/schooladmin/images/schoollogo.png); background-repeat:no-repeat; width:36px; height:61px;">&nbsp;</td> 
    <td colspan="2"><?php echo $this->schoolDetails['name'] ?></td> 
    </tr> 
<?php 
    if($user){ 
    $user['userdetails']['name'] = $user['userdetails']['firstname'].$user['userdetails']['lastname']; 
    $photoPath = BASE_PATH."/uploads/photos/student/".$user['userdetails']['photo']; 
    if(!file_exists($photoPath)){ 
     $photoPath = BASE_PATH."/skins/schooladmin/images/id_photo.png"; 
    } 
    }else{ 
     $photoPath = BASE_PATH."/skins/schooladmin/images/id_photo.png"; 
    } 
?> 
    <tr> 
    <td style="background-image: url(<?php echo $photoPath; ?>); background-repeat:no-repeat; width:82px; height:82px;">&nbsp;</td> 
    <td colspan="2"> 
    <table width="100%" border="0" cellspacing="3" cellpadding="3" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; font-weight:bold; color:#fff;" > 
    <?php if($this->templateDetails['template']){ 
     for($i=0;$i<1;$i++){ 
      for($j=0;$j<count($this->templateDetails['template_details']);$j++){ 
       if($this->templateDetails['template_details'][$j]['type'] == "text" && $this->templateDetails['template_details'][$j]['page'] == "front"){ 
    ?> 
     <tr> 
     <td width="38%"><?php echo $this->templateDetails['template_details'][$j]['label']; ?></td> 
     <td width="62%"><?php echo @$user['userdetails'][$this->templateDetails['template_details'][$j]['field']]; ?></td> 
     </tr> 
    <?php } } } } ?> 
    </table> 
    </td> 
    </tr> 
    <tr> 
     <td colspan="3" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; font-weight:bold; color:#fff;" align="center"><?php echo $this->schoolDetails['address'] ?>, <?php echo $this->schoolDetails['address2'] ?>, <?php echo $this->schoolDetails['city'] ?> - <?php echo $this->schoolDetails['zipcode'] ?></td> 
    </tr> 
    <tr> 
     <td colspan="3" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; font-weight:bold; color:#fff;" align="center">Ph: <?php echo $this->schoolDetails['contactno'] ?></td> 
    </tr> 
</table> 
    </div> 

    <div style="background-image: url(http://192.168.2.26/skins/schooladmin/images/id_bg.png); background-repeat: repeat; width:400px; height:220px;margin:10px 0px 0px 10px;float:left;position:relative;"> 
<table style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:14px; font-weight:bold; color:#fff; background-image:url(http://192.168.2.26/skins/schooladmin/images/id_white_bg.png); background-repeat:no-repeat;" width="100%" border="0" cellpadding="5" cellspacing="5"> 
    <?php if($this->templateDetails['template']){ 
     for($i=0;$i<1;$i++){ 
      for($j=0;$j<count($this->templateDetails['template_details']);$j++){ 
       if($this->templateDetails['template_details'][$j]['type'] == "text" && $this->templateDetails['template_details'][$j]['page'] == "back"){ 
    ?> 
     <tr> 
     <td width="38%"><?php echo $this->templateDetails['template_details'][$j]['label']; ?></td> 
     <td width="62%"><?php echo @$user['userdetails'][$this->templateDetails['template_details'][$j]['field']]; ?></td> 
     </tr> 
    <?php } } } } ?> 
</table> 
    </div> 
    <?php } } ?> 
    </body> 
</html> 
+0

我不知道DOMPDF很好地處理CSS或表。您是否嘗試過使用其他PDF庫,如[TCPDF](http://www.tcpdf.org/)或[MPDF](http://www.smaizys.com/php/mpdf-html-to-pdf-介紹/)? – PenguinCoder

+0

我曾使用TCPDF,它運行良好。 – Venu

+0

TCPDF不支持我 –

回答

0

對於geeting 100%相同的觀點,使用PHP FPDF library.You能獲得所有的支持,教程並從here下載

+0

好的謝謝......讓我試着讓你知道...... –

1

我建議不要試圖按摩您的HTML,以便它以這種方式轉換爲PDF。當您切換渲染引擎甚至打印機時,您將花費很多精力來調整可能會改變的事情。

如果您創建您的PDF原生使用FPDFTCPDF你將有一個更容易的時間。例如(只是爲了讓你開始):

<?php 

require_once('/path/to/tcpdf.php'); 

// misc variables 
$lightblue = array(100, 100, 250); 
$darkblue = array(0, 0, 255); 
$white = array(255, 255, 255); 

// prepare new PDF document 
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); 
$pdf->SetFont('helvetica', 'B', 20); 

// add a page 
$pdf->AddPage(); 

// set the coordinates of the gradient 
$coords = array(0, 1, 1, 0); 

// paint a linear gradient 
$pdf->LinearGradient(20, 45, 105, 60, $lightblue, $darkblue, $coords); 

// write text 
$pdf->SetTextColorArray($white); 
$pdf->Text(40, 50, 'hello'); 
$pdf->Text(40, 60, 'world'); 

// Close and output PDF document 
$pdf->Output('tkexample.pdf', 'I');