以PDF格式显示数据

时间:2016-01-28 07:05:13

标签: php dompdf

为什么我无法在PDF文件中显示数据库中回显的数据? 我用DOMPDF来转换我的html。有人请帮忙。

以下是PDF结果

PDF result image

这是连接代码

  <?php


  $id = $_GET['id'];

  $conn = pg_connect("host=localhost dbname=mydatabase user=postgres password=admin");
  if (!$conn) {
  echo "An error occurred. cannot connectect to localhost.\n";
  exit;
  }

  $sql = pg_query($conn, "SELECT * FROM mytable WHERE reference_no = $id ");
    if (!$sql) {
      echo "Error Ariel 7:50 !!!.\n";

    }

    while ($row = pg_fetch_assoc($sql)) {
      $reference_no = $row['reference_no'];
      $wo_name = $row['wo_name'];

    }

这是我的转换代码:

error_reporting(0);
    require_once ('xdompdf/dompdf_config.inc.php');
    $pdf_content='
                <style type="text/css">

                div.refno{padding-left: 190px;}
                   .line{padding-left: 260px; padding-top: -37px;}

               </style>

     <body>
     <div>

         <h4> Work Order Form </h4>
         <p> Reference No.          <div class="refno"><?php echo        $reference_no;?></div>     </p>
        <p class="line">:_________________________________________________</p>

        <p> Work Order Name         <div class="woname"><?php echo $wo_name;?></div>        </p> 
        <p  class="line">:_________________________________________________</p>
    </div>

    </body>'
        ;
        $name = date("Ymd").rand().'.pdf';
        $reportPDF=createPDF(12, $pdf_content, 'activity_Report', $name );
        function createPDF($pdf_userid, $pdf_content, $pdf_For, $filename){

        $path='xdompdf/';
      /*$rndNumber=rand();
        $filename=$pdf_userid.date("Ymd").$rndNumber.'.pdf';*/
        $dompdf=new DOMPDF();
        $dompdf->load_html($pdf_content);
        $dompdf->render();
        $output = $dompdf->output();
        file_put_contents($path.$filename, $output);
        return $filename;       
        }   
        echo '<a href="xdompdf/'.$name.'" > Download </a>';
      ?>

请帮我告诉我如何显示数据。

2 个答案:

答案 0 :(得分:1)

$pdf_content更改为

$pdf_content='
    <style type="text/css">
    div.refno{padding-left: 190px;}
    .line{padding-left: 260px; padding-top: -37px;}
    </style>
    <body>
        <div>
            <h4> Work Order Form </h4>
            <p> Reference No.          <div class="refno">' . $reference_no . '</div>     </p>
            <p class="line">:_________________________________________________</p>

            <p> Work Order Name         <div class="woname">' . $wo_name . '</div>        </p>
            <p  class="line">:_________________________________________________</p>
        </div>
    </body>';

答案 1 :(得分:1)

我认为您只需要在$ pdf_content中汇总数据:

$pdf_content='
            <style type="text/css">

            div.refno{padding-left: 190px;}
               .line{padding-left: 260px; padding-top: -37px;}

           </style>

 <body>
 <div>

     <h4> Work Order Form </h4>
     <p> Reference No.          <div class="refno">'.$reference_no.'</div>              </p>
    <p class="line">:_________________________________________________</p>

    <p> Work Order Name         <div class="woname">'.$wo_name.'</div>        </p> 
    <p  class="line">:_________________________________________________</p>
</div>

</body>';