简体   繁体   中英

How to change the DOMPDF_ENABLE_PHP

I'm having a problem converting HTML to PDF (using Dompdf ) 0.8.0 and I've been searching for an answer. I found that I need to change the DOMPDF_ENABLE_PHP to true . My problem is that I don't know where to change that or how.

Sorry if this is a stupid question; I'm still a rookie.

Here's my php code

<?php

require __DIR__."/../vendor/autoload.php";

use Dompdf\Dompdf;

//generate some PDFs!
$dompdf = new DOMPDF();  //if you use namespaces you may use new \DOMPDF()
$dompdf->loadHtml(file_get_contents("re1.php"));
$dompdf->render();
$dompdf->stream("sample.pdf", array("Attachment"=>0));
?>

my re1.php

<?php require '../db.php';?>
<!DOCTYPE html>
<html>
 <head>
<title></title>
<?php include '../templates/links.inc';?>
</head>
<body>

<div class="container">
<?php
   $countr=0;                                            
   $sline = explode(",", $_SESSION["hroomid"]);
   foreach ($sline as $id) {
   $result = $mysqli->query("SELECT r.*,rt.* FROM tbl_room r INNER JOIN 
tbl_roomtype rt ON r.`RoomTypeID` = rt.`RoomTypeID`  WHERE RoomID = 
'$sline[$countr]'");
   $row = $result->fetch_assoc();
   echo $row['RoomNumber'];
   $countr++;
   }
?>
</div>
</body>
</html>

the output on pdf:

query("SELECT r.*,rt.* FROM tbl_room r INNER JOIN tbl_roomtype rt ON 
r.`RoomTypeID` =
rt.`RoomTypeID` WHERE RoomID = '$sline[$countr]'"); $row = $result-
>fetch_assoc(); echo
$row['RoomNumber']; $countr++; } ?>

Dompdf as of 0.7.0 no longer uses the configuration constants. To change the configuration you either pass in an option when you instantiate your object:

$dompdf = new Dompdf(array('isPhpEnabled' => true));

or set the value on the instance:

$dompdf->setOption('isPhpEnabled', true);

However, please note that as of 0.6.2 Dompdf will no longer parse a PHP document for security reasons. This means that you can't do things the way you want. This change was made to address security concerns.

The 'isPhpEnabled' option only enables support for embedded PHP , ie PHP code inside <script type="text/php"></script> . Embedded PHP is not able to modify the structure of the document, except as you use it to interact with the canvas object or PDF rendering library directly. And the Dompdf team recommends you avoid enabling this option if you can.

You can still do what you want, but you'll need to modify your PDF-generating PHP code to be something like the following:

<?php

require __DIR__."/../vendor/autoload.php";

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

use Dompdf\Dompdf;

//generate some PDFs!
$dompdf = new DOMPDF();  //if you use namespaces you may use new \DOMPDF()
$dompdf->loadHtml($html);
$dompdf->render();
$dompdf->stream("sample.pdf", array("Attachment"=>0));
?>

I tried making a receipt this way. But it was not possible to convert the php webpage to pdf using ob_get_clean() if it contained some unique ID in the url (such as receipt.php?uid=1). I found creating the receipt directly in this page more easy, as compared to converting a different php page.

You can write directly inside loadHtml. However, the commands and functions should be written separately.

You can write this way- "The customer's name is ".$row['name']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM