【问题标题】:Dompdf not rendering content inside php tagsDompdf 未在 php 标签内呈现内容
【发布时间】:2019-07-08 20:29:52
【问题描述】:

Dompdf 正在渲染我的 HTML 页面,但没有保持样式,也没有渲染 php 标记内的任何内容。

我正在尝试渲染当前页面:

但是当我使用 dompdf 时,我得到以下结果:

您在第一张图片上看到的完整 A4 预览是在 php 标记内创建的:

try {
    require "config.php";
    require "common.php";
    $id = $_GET['cvid'];
    $connection = new PDO($dsn, $username, $password, $options);
    $sql = "SELECT * FROM cvs WHERE id = :cvid";
    $statement = $connection->prepare($sql);
    $statement->bindParam(':cvid', $id, PDO::PARAM_STR);
    $statement->execute();
    $result = $statement->fetchAll();
}
catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
?>

<?php include "header.php"; ?>

<p class="hidden" id="hiddencvid"><?php echo $id ?></p>



<button id="updatecv" name="updatecv" class="cvupdater">Opdater CV</button>
<form action="createpdf.php">
<button id="makepdf" name="makepdf" class="cvupdater">Lav PDF</button>
</form>
<p class="cvupdater" style="width:500px;">Tryk på tekst for at redigere. Tryk på "Opdater CV" for at gemme ændringer.</p>

<a href="index.php">Hjem</a>

<div class="cvpreview">

<?php foreach($result as $row) { ?>


<div class="cvheader">
  <div id="img-wrapper">
    <img src="uploads/<?php echo escape($row["cvid"]) . ".png" ?>" alt="profile_picture">
  </div>
  <h1><?php echo escape($row["fullname"])?></h1>
  <div contenteditable class="cvheader-worktitle" id="worktitle"><?php $decodedworktitle = html_entity_decode($row["worktitle"]); echo $decodedworktitle?></div>
</div>

<hr>

<div class="cvbody">
    <div class="leftcolumn">
      <div class="profil columnboxleft">
        <h5>Profil</h5>
        <div contenteditable class="cvtext" id="profiledesc"><?php $decodedprofiledesc = html_entity_decode($row["profiledesc"]); echo $decodedprofiledesc?></div>
      </div>   
      <div class="workexperience columnboxleft">
        <h5>Arbejdserfaring</h5>  
        <div contenteditable class="cvtext" id="workexperience"><?php $decodedworkexperience = html_entity_decode($row["workexperience"]); echo $decodedworkexperience?></div>  
      </div>
      <div class="education columnboxleft">
        <h5>Uddannelse</h5>
        <div contenteditable class="cvtext" id="education"><?php $decodededucation = html_entity_decode($row["education"]); echo $decodededucation?></div>  
      </div>
    </div>

    <div class="rightcolumn">
      <div class="details columnboxright">
        <h5>Detaljer</h5>
        <div contenteditable class="cvtext" id="phonenumber"><?php $decodedphonenumber = html_entity_decode($row["phonenumber"]); echo $decodedphonenumber?></div>
        <div contenteditable class="cvtext" id="email"><?php $decodedemail = html_entity_decode($row["email"]); echo $decodedemail?></div>
        <div contenteditable class="cvtext" id="birthdate"><?php $decodedbirthdate = html_entity_decode($row["birthdate"]); echo $decodedbirthdate?></div>
      </div>
      <div class="skills columnboxright">
        <h5>Evner</h5>
        <div contenteditable class="cvtext" id="skills"><?php $decodedskills = html_entity_decode($row["skills"]); echo $decodedskills?></div>
      </div>
      <div class="languages columnboxright">
        <h5>Sprog</h5>
        <div contenteditable class="cvtext" id="languages"><?php $decodedlanguages = html_entity_decode($row["languages"]); echo $decodedlanguages?></div>
      </div>
    </div>
</div>

<?php } ?>
</div>

<?php include "footer.php"; ?>

我还有另一个 php 文件来创建 pdf(createpdf.php):

require '../vendor/autoload.php';

ob_start();
include "cvmaker.php";
$html = ob_get_clean();
ob_end_clean();

use Dompdf\Dompdf;
define('DOMPDF_ENABLE_PHP', true);

$options = new \Dompdf\Options();
$options->setIsPhpEnabled(true);
$dompdf = new Dompdf($options);
$dompdf->loadHtml($html);
$dompdf->setPaper('a4', 'portrait');
$dompdf->render();
$dompdf->stream('hello.pdf');

?>

我尝试过使用不同的 dompdf 选项,但我一直返回相同的结果。如何让它呈现我的嵌入式 php?

【问题讨论】:

  • 在 iframe 中显示完整的 pdf
  • @FAEWZX 抱歉,你能详细说明一下吗,我之前没用过 iframe
  • &lt;iframe src="script_show_pdf.php" height="200" width="300"&gt;&lt;/iframe&gt; w3schools.com/HTML/html_iframe.asp

标签: php html dompdf


【解决方案1】:

您的 PHP 和 HTML 表面上看起来不错。看起来您缺少内容,因为生成 PDF 的按钮未传递 $id 变量。您至少应该将表单修改为以下内容:

<form action="createpdf.php?id=<?=$id?>">
  <button id="makepdf" name="makepdf" class="cvupdater">Lav PDF</button>
</form>

此外,您实际上并没有对您的表单做任何事情(即不是要传递的表单字段),因此您也可以将其设为链接。使用像 Bootstrap 这样的 UI 框架时会更容易一些,因为您可以设置链接样式,使它们看起来像按钮。

至于 CSS,它实际上取决于您如何引用外部文件(如果它是外部文件)。查看您提供给 Dompdf 以调试该部分问题的 HTML 会有所帮助。

【讨论】:

  • 旁注:虽然这可能只是为了开发,但练习最佳实践总是好的。我一直在呼应一些事情,但这并不是一个好习惯。也许你不会在你的实时服务器上这样做,但如果我至少没有指出这一点,我将是失职。我尝试提醒自己使用日志记录并跟踪日志,以便看到错误流入。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-20
  • 2022-01-24
  • 1970-01-01
  • 2014-06-24
  • 1970-01-01
  • 2021-08-12
  • 1970-01-01
相关资源
最近更新 更多