简体   繁体   中英

Convert HTML with inline PHP to PDF

Created a small application in which one can place orders for coffee, and orders along with the date they were ordered are saved into a database.

I have a page, report.php, that generates reports based on date constraints from user input coming from an admin page, adminPage.html.

adminPage.html passes the date constraints to report.php using GET function, and report.php generates a report using PHP with those constraints.

I am having trouble converting the report page into a PDF. I tried using dompdf, however, it would not load the PHP report in the PDF, only the HTML code.

Function where the user clicks the button to run the report on 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+"");

}

The window.open opens the report page with the constraints loaded into the URL. I know it's not pretty, sorry :(

Here is the report page I am trying to generate into a PDF. I was hoping to have a small button at the bottom of the screen that says "generate PDF" or something so when the user presses it, it thusly generates the 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>

This is a basic example of how your could would look like if you were using 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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM