使用PHP转换为PDF

时间:2014-05-14 20:32:34

标签: php pdf

所以我使用这个API(http://phptopdf.com/)将一些php文本转换为PDF。 但是我收到了这个错误: -

一切正常,直到第一个名字,电子邮件。 但是,当我尝试链接订单时,一切都变得混乱。

Parse error: syntax error, unexpected T_WHILE in /home/a/public_html/pdf/packing_slip.php on line 124

代码

<?php
session_start();
include_once('phpToPDF.php') ;
include '../dbconnector.php';
include_once '../inc/inc.functions.php';
include '../dbpdo.php';
//require 'fbconfig.php';
if(!isset($_SESSION['email']))
{
    //not logged in.
    header('Location:http://macbuyback.co.uk/register');
    exit();
}
if((isset($_SESSION['logged']) && $_SESSION['logged']=1) || $user)
{
  //load the record for last orders
  $user_email = $_SESSION['email'];
  $date=date('Y-m-d');
  try
  {
    $statement = $conn->prepare("SELECT * From sales where email =? and orderDate = ?");
    $statement->execute(array(
      $user_email,
      $date));
    $statement->setFetchMode(PDO::FETCH_ASSOC);
  }
  catch(PDOException $e)
  {
    echo $e->getMessage();
  }

  //now get the customer details
  try
  {
    $statement2 = $conn->prepare("SELECT * From users where email = ?");
    $statement2->execute(array(
      $user_email));
    $statement2->setFetchMode(PDO::FETCH_ASSOC);
    $user_info = $statement2->fetch();
  }
  catch(PDOException $e)
  {
    echo $e->getMessage();
  }

  // Assign html code into php variable:-
    $html = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Company</title>
<style type="text/css">
<!--
body {
  font-family:Tahoma;
}

img {
  border:0;
}

#page {
  width:800px;
  margin:0 auto;
  padding:15px;

}

#logo {
  float:left;
  margin:0;
}

#address {
  height:181px;
  margin-left:250px; 
}

table {
  width:100%;
}

td {
padding:5px;
}

tr.odd {
  background:#e1ffe1;
}
-->
</style>
</head>
<body>
<div id="page">
  <div id="logo">
    <a href="http://www.domain.co.uk/"><img src="http://www.domain.co.uk/pdf/logo.png"></a>
  </div><!--end logo-->

  <div id="address">

    <p>Company<br />
    <a href="mailto:info@company">info@company</a>
    <br /><br />
    Created on ' . date("Y-m-d") . ' 
    echo ' . $a . '
    </p>

  </div><!--end address-->

  <div id="content">
    <p>
      <strong>Customer Details</strong><br />
      Name: ' . $user_info['firstname'] . '<br />
      Email: ' . $_SESSION['email'] . '<br />

    <hr> ' . $a=1 .'
    <table>
      <tr>
        <td><strong>Serial</strong></td>
        <td><strong>Product Name</strong></td>
        <td><strong>Amount</strong></td>
        <td><strong>Pickup Date</strong></td>
      </tr>
      ' . while($row = $statement->fetch()){ . '
      '  if(($a%2) == 0){ . '
      <tr class="odd">
        <td> ' . $a . '</td>
        <td> ' . getProductNameFromId($row['pid']) . '</td>
        <td>' . $row['amount'] . '</td>
        <td>' . $row['shipmentdate'] . '</td>
      </tr>
      ' . $a++ }else{. '
      <tr class="even">
        <td> ' . $a . '</td>
        <td> ' . getProductNameFromId($row['pid']) . '</td>
        <td>' . $row['amount'] . '</td>
        <td>' . $row['shipmentdate'] . '</td>
      </tr>
      ' . $a++ }. '
      ' . } . '
    </table>
    <hr>
    <p>
      Thank you for your order!  This transaction will appear on your billing statement as "Your Company".<br />
      If you have any questions, please feel free to contact us at <a href="mailto:youremail@somewhere.com">youremail@somewhere.com</a>.
    </p>

    <hr>

    <p>
      <center><small>This communication is for the exclusive use of the addressee and may contain proprietary, confidential or privileged information. If you are not the intended recipient any use, copying, disclosure, dissemination or distribution is strictly prohibited.
      <br /><br />
      &copy; Your Company All Rights Reserved
      </small></center>
    </p>
  </div><!--end content-->
</div><!--end page-->
</body>

</html>';

phptopdf_html($html,'pdf', 'sample.pdf');
echo "<a href='pdf/sample.pdf'>Download PDF</a>";
}//login check
else
{
    header('Location:http://domain.co.uk/register');
    exit();
}
?>

可能有什么问题? 欢迎任何建议。

1 个答案:

答案 0 :(得分:1)

你的while循环不能在字符串连接中使用。

$html ='.....
</tr>';
      while($row = $statement->fetch()){
        if(($a%2) == 0){
      $html .= '<tr class="odd">
        <td> ' . $a . '</td>
        <td> ' . getProductNameFromId($row['pid']) . '</td>
        <td>' . $row['amount'] . '</td>
        <td>' . $row['shipmentdate'] . '</td>
      </tr>';
      $a++; }else{
      $html .= '<tr class="even">
        <td> ' . $a . '</td>
        <td> ' . getProductNameFromId($row['pid']) . '</td>
        <td>' . $row['amount'] . '</td>
        <td>' . $row['shipmentdate'] . '</td>
      </tr>';
       $a++; }
       } 
   $html .= ' </table>
 .....';