php - 使用内联 PHP 将 HTML 转换为 PDF
问题描述
创建了一个小型应用程序,可以在其中下订单咖啡,并将订单连同订购日期一起保存到数据库中。
我有一个页面report.php,它根据来自管理页面adminPage.html 的用户输入的日期约束生成报告。
adminPage.html 使用 GET 函数将日期约束传递给 report.php,report.php 使用带有这些约束的 PHP 生成报告。
我在将报告页面转换为 PDF 时遇到问题。我尝试使用 dompdf,但是,它不会在 PDF 中加载 PHP 报告,只会加载 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 的报告页面。我希望在屏幕底部有一个小按钮,上面写着“生成 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>
解决方案
这是一个基本示例,说明如果您使用 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
推荐阅读
- autodesk-forge - 尝试复制文件时出错
- android - 当按下设备上的“物理”后退按钮时,在片段中调用反向 MotionLayout 动画
- node.js - 初始化前无法访问“应用程序”[VS code javascript webdev]
- robotframework - 如何将机器人自定义库名称作为关键字参数?
- google-apps-script - 多次运行 Google Apps 脚本时,Google 会形成重复部分
- node.js - Vue CLI SOCKET_TIMEOUT 创建新项目时出错(npm 错误)
- terraform - 减少 Terragrunt 控制台输出
- python - 在 Pandas 中更改数据框列 dtypes
- applescript - 在苹果脚本中将一个整数乘以另一个整数一定次数
- hazelcast - 循环访问从 Hazelcast 的 ClientConfig 实例创建的 mapConfig 时出现 UnsupportedOperationException