php - 在我通过 php 获取当前页面的代码之前,如何加载并准备好 HTML 页面?
问题描述
我这里有两页。我使用第一页(mpdfData.php)来获取数据。然后我将数据发送到第二页 (mpdfPage.php) 以创建一个 HTML 页面,其中包含我从第一页发送的数据。
这个想法是形成第二页并通过第一页中的 PHP 代码将其从 HTML 转换为 PDF 来打印 PDF。
我面临的问题是,当我将第二页的 HTML 代码返回到第一页时,我从第一页发送的值确实会显示出来,而不是代码本身<?php echo $user?>
并<?php echo $pass?>
显示在目标 PDF 中。
我该如何克服这个?
<!-- mpdfData.php -->
<?php
$html="";
if (isset($_POST['submit'])) {
$user=$_POST['userName'];
$pass=$_POST["password"];
?><div hidden><?php
include 'mpdfPage.php';
?><div><?php
}
if ($html !== '') {
require_once __DIR__ . '/vendor/autoload.php';
// Create an instance of the class:
$mpdf = new \Mpdf\Mpdf();
// Write some HTML code:
$mpdf->WriteHTML($html);
// Output a PDF file directly to the browser
$mpdf->Output();
}
?>
<html>
<head>
<title>User Login</title>
<link rel="stylesheet" type="text/css" href="styles1.css" />
</head>
<body>
<form name="frmUser" method="post" action="">
<div class="message1"><h2>PDF PAGE:</h2></div>
<table border="0" cellpadding="10" cellspacing="1" width="500" align="center" class="tblLogin">
<tr class="tableheader">
<td align="center" colspan="2">Enter PDF Details</td>
</tr>
<tr class="tablerow">
<td>
<input type="text" name="userName" placeholder="User Name" class="login-input"></td>
</tr>
<tr class="tablerow">
<td>
<input type="password" name="password" placeholder="Password" class="login-input"></td>
</tr>
<tr class="tableheader">
<td align="center" colspan="2">
<input type="submit" name="submit" value="Print Pdf" class="btnSubmit"></td>
</tr>
</table>
</form>
</body>
</html>
<!-- mpdfPage.php -->
<html>
<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<h2>HTML Table</h2>
<form name="table" method="post" action="">
<table>
<tr>
<th>User Name</th>
<th>Password</th>
</tr>
<tr>
<td><?php echo $user?></td>
<td><?php echo $pass?></td>
</tr>
</table>
</form>
</body>
</html>
<?php
$html = file_get_contents(__FILE__);
?>
解决方案
由于file_get_contents(__FILE__)
不执行 PHP 代码而遇到的问题,它将提供文件的原始内容。您可以使用以下方式实现这一点。
<?php ob_start(); ?>
<html>
<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<h2>HTML Table</h2>
<form name="table" method="post" action="">
<table>
<tr>
<th>User Name</th>
<th>Password</th>
</tr>
<tr>
<td><?php echo $user?></td>
<td><?php echo $pass?></td>
</tr>
</table>
</form>
</body>
</html>
<?php $html = ob_get_clean(); ?>
推荐阅读
- mysql - 我将如何在 SQL / SQLALCHEMY 中设计相互依赖的外键
- javascript - 在 React-Redux 中实时重新排序列表的最佳方法?
- qliksense - Qliksense ::计算尺寸
- twilio -
动词引发“发现以元素'Pay'开头的无效内容。'{Play”之一 - c# - 如何将 ExecuteScalar 与存储过程一起使用?
- bash - bash:bash 脚本给出了错误的替换错误
- angular - Angular 7 在应用程序主组件的后台运行多个方法会减慢负载吗?
- python - 切片列表等待并走得更远
- android - ConstraintLayout - 在根中居中 TextView 但不覆盖 Button
- sql - 使用主键且多个重复项在另一个表中的 SQL 中的 INSERT INTO SELECT 查询