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:
But as I use dompdf I get following result:
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?
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.