从PHP和PDF转换

时间:2012-05-23 13:11:18

标签: php html pdf dompdf

我需要从Php(html Table)转换为Pdf。我已经使用DOM PDf进行转换。但我没有在pdf中得到正确的观点。请找到下面的图片。

Pdf View

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>

2 个答案:

答案 0 :(得分:1)

我建议不要尝试按下HTML,以便以这种方式转换为PDF。当您切换渲染引擎甚至打印机时,您将花费很多精力来调整可能会发生变化的事情。

如果您使用FPDFTCPDF本地创建PDF,您将会更轻松。例如(只是为了让你入门):

<?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');

答案 1 :(得分:0)

对于100%相同视图的geeting,请使用php fpdf库。您可以从here获得所有支持,教程和下载