Nick Alton
Nick Alton

Reputation: 21

dompdf and php (mysql data)

I would like to pull data from a mysql db. This data is then inserted into a html file which is then converted to a pdf using dompdf.

The template is perfect and display's well when I run call dompdf.

However as soon as I try and insert php code, the template still shows perfectly, how ever the php code is displays nothing. If I open the page its shows, so I know it works.

In the options file I have done this :

private $isPhpEnabled = true;

my php file to call the template (LeaseBase.php):

<?php
$options = new Options(); $options->set('isPhpEnabled','true');


$leasefile = file_get_contents("Leases/LeaseBase.php"); 
$dompdf = new Dompdf($options); $dompdf->loadHtml($leasefile);


$dompdf->stream(); $output = $dompdf->output(); file_put_contents('Leases/NewLeases.pdf', $output);

?>

I also can't seem to pick up anything in the log files.

Any assistance is appreciated

Upvotes: 2

Views: 2735

Answers (2)

BrianS
BrianS

Reputation: 13944

With versions of Dompdf prior to 0.6.1 you could load a PHP document and the PHP would be processed prior to rendering. Starting with version 0.6.1 Dompdf no longer parses PHP at run time. This means that if you have a PHP-based document you have to pre-render it to HTML, which does not happen when using file_get_contents().

You have two options:

First: Use output buffering to capture the rendered PHP.

ob_start();
require "Leases/LeaseBase.php";
$leasefile = ob_get_contents();
ob_end_clean();

Second: Fetch the PHP file via your web server:

$leasefile = file_get_contents('http://example.com/Leases/Leasebase.php');

...though, actually, if I were loading the file into a variable and feeding it to dompdf without doing any further manipulation I would use dompdf to fetch the file instead. In this way you are less likely to have to deal with external resource (images, stylesheets) reference problems:

$dompdf->load_html_file('http://example.com/Leases/Leasebase.php');

Upvotes: 0

Webeng
Webeng

Reputation: 7143

However as soon as I try and insert php code, the template still shows perfectly, how ever the php code is displays nothing.

Answer: It shows nothing because when a php page is executed, it outputs html (and not the php code). If you don't have an echo or print or any code that generates html code from the php script, the page will in fact be blank.

It's important to remember that php is serverside code WHICH CAN generate html code as long as you instruct it accordingly.

Upvotes: 1

Related Questions