dongnuo4594 2019-02-14 18:17
浏览 66

Dompdf不在php标签内呈现内容

Dompdf is rendering my HTML page, but without maintaining style, as well as not rendering anything that is inside a php tag.

I'm trying to render current page:

enter image description here

But as I use dompdf I get following result:

enter image description here

The complete A4 preview as you see on the first image is created inside a php tag:

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"; ?>

And I have another php file to create the 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');

?>

I've tried meddling with different dompdf options, but I keep returning at the same result. How do I get it to render my embedded php?

  • 写回答

1条回答 默认 最新

  • duankuan5319 2019-02-17 00:01
    关注

    Your PHP and HTML look OK on the surface. It looks like you're missing your content because the button to generate the PDF doesn't pass the $id variable. You should, at the very least, modify the form to something like the following:

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

    Also, you're not actually doing anything with your form (i.e. not form fields to pass) so you could just as well make that a link. A bit easier to do when using a UI framework like Bootstrap since you can style links so that they appear to be buttons.

    As for the CSS, it really depends on how you're referencing external file (if it is external). It would help to see the HTML that you're feeding to Dompdf to debug that part of the problem.

    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制