使用嵌入式PHP将HTML转换为PDF

时间:2018-07-03 10:06:30

标签: php html pdf

创建了一个小型应用程序,可以在其中放置咖啡订单,并将订单及其订购日期保存到数据库中。

我有一个页面report.php,该页面根据来自管理页面adminPage.html的用户输入的日期约束来生成报告。

adminPage.html使用GET函数将日期约束传递给report.php,然后report.php使用具有这些约束的PHP生成报告。

我无法将报告页面转换为PDF。我尝试使用dompdf,但是,它不会将PHP报告加载到PDF中,而只会加载HTML代码。

用户单击按钮可在adminPage.html上运行报告的功能:

function runReport()
{
var jStartDate;
var jEndDate;
jStartDate = document.getElementById("reportStartDate").value;
jEndDate = document.getElementById("reportEndDate").value;

window.open("report.php?startDate="+jStartDate+"&endDate="+jEndDate+"");

}

window.open打开报告页面,并将约束加载到URL中。我知道那不漂亮,很抱歉:(

这是我要生成为PDF的报告页面。我希望屏幕底部有一个小按钮,上面写着“ generate PDF”(生成PDF),这样当用户按下它时,它就会生成PDF。

<!DOCTYPE HTML>
<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"> 
</script>

<!-- allow login page to link to this when user is an admin -->
<title>Report</title>

<link rel="stylesheet" type="text/css" href="styles.css">
<link rel="manifest" href="/manifest.json">

<center><img src="icons/sbuxLogo.png" width="100" height="100px"></center>
</br></br>
<center><h1>Welcome Administrator!</h1></center>
</br></br>
<center>

<div id="report">

<h2>Report:</h2>
</br>
</center>

<?php
require "phpScripts/connect.php";

$startDate = $_GET['startDate'];
$endDate = $_GET['endDate'];

$sql = "SELECT * FROM orderlist WHERE NOT (dateOrdered < '$startDate' OR dateOrdered > '$endDate');";

$result = $conn->query($sql);

echo "<center>";
echo "<ul>";
if($result->num_rows >0)
{
  $i = 0;
  while($row = $result->fetch_assoc()) // this loads selected drinks via date 
into ul
  {
    echo  "<li style=
    'margin-left: 0;
    list-style-type: none;
    color: #727272;
    height: 40px;
    font-size: 15px;
    font-family: Open Sans;
    border-style: none;
    width: 50%;
    list-style-type: none;
    margin:0;
    padding-bottom:0px;
    padding:0;
    overflow: hidden;'>

    Drink: ".$row["drinkName"]. ", Date Ordered: " .$row["dateOrdered"] . ", 
Cost: " .$row["drinkCost"] . "</li>";
  $i = $i+1;
  }
}else {
  echo "<p> No orders found. </p>";
}
echo "</ul>";
echo "</center>";
?>

</div>

1 个答案:

答案 0 :(得分:0)

这是一个基本示例,说明您使用TCPDF时的样子

<?php
$pdf = new TCPDF(PDF_PAGE_ORIENTATION_L, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
//TCPDF_INIT_CODE_GOES_HERE 
// ...

$html = '<!DOCTYPE HTML>
<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"> 
</script>

<!-- allow login page to link to this when user is an admin -->
<title>Report</title>

<link rel="stylesheet" type="text/css" href="styles.css">
<link rel="manifest" href="/manifest.json">

<center><img src="icons/sbuxLogo.png" width="100" height="100px"></center>
</br></br>
<center><h1>Welcome Administrator!</h1></center>
</br></br>
<center>

<div id="report">

<h2>Report:</h2>
</br>
</center>';


require "phpScripts/connect.php";

$startDate = $_GET['startDate'];
$endDate = $_GET['endDate'];

$sql = "SELECT * FROM orderlist WHERE NOT (dateOrdered < '$startDate' OR dateOrdered > '$endDate');";

$result = $conn->query($sql);

$html .= "<center>";
$html .= "<ul>";
if($result->num_rows >0)
{
    $i = 0;
    while($row = $result->fetch_assoc()) // this loads selected drinks via date 
    into ul
    {
        $html .=  "<li style=
        'margin-left: 0;
        list-style-type: none;
        color: #727272;
        height: 40px;
        font-size: 15px;
        font-family: Open Sans;
        border-style: none;
        width: 50%;
        list-style-type: none;
        margin:0;
        padding-bottom:0px;
        padding:0;
        overflow: hidden;'>

        Drink: ".$row["drinkName"]. ", Date Ordered: " .$row["dateOrdered"] . ", 
        Cost: " .$row["drinkCost"] . "</li>";
        $i = $i+1;
    }
}else {
    $html .= "<p> No orders found. </p>";
}
$html .= "</ul>";
$html .= "</center>";
$html .= '</div>';

$pdf->writeHTML($html);
$pdf->Output('TITLE_OF_PDF_FILE.pdf', 'I'); // I flag means it will display the PDF in a browser