dompdf and php (mysql data) - php

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

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.

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');

Related

I cannot open pdf in chrome created by Mpdf - CodeIgniter . - Failed to load PDF document

I am new to PHP report generation. I am trying to use MPDF 5.7. When I try create sample pdf using simple php page, it is created successfully. But when I put it in to codeigniter chrome browser says "Failed to load PDF document.". But It can open using firefox. But if i downloaded it, it is not support to open using adobe reader. But still my sample pfd is working well anyway.
This is how I create sample pdf.
<?php
$html = '
<h1>mPDF</h1>
<h2>Basic Example Using CSS Styles</h2>
<p class="breadcrumb">Chapter ยป Topic</p>
<h3 style="color:red; background-color:gray; margin-left:100px;">Heading 3</h3>';
include("themes/MPDF57/mpdf.php");
$mpdf=new mPDF('c');
$mpdf->SetDisplayMode('fullpage');
// LOAD a stylesheet
$stylesheet = file_get_contents('mpdfstyleA4.css');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
?>
When I put it into codeigniter cannot open pdf. my php file is created in 'view' and mpdf library also put in same place in theme folder.
Your PDF file contains some direct PHP output from your script which makes it corrupt.
It can be a whitespace from after the ?> PHP closing tag, it can be PHP notice printed during course of generating the file, etc.
Be sure that you disable all layouting your framework provides as it produces the exact same output that could cause this.
To troubleshoot further, save your PDF file to disc ($mpdf->Output('<file path>', "D");) and open it in a text editor. You will see a bunch of weird characters.
If the file does not start with %PDF-1.4, look for the reason of the output before calling $mpdf->Output();.
If there is some readable text at the end of the document, look after calling the Output method.
I'd suggest you to remove your ?> PHP closing tag anyway for a good measure.
See also https://mpdf.github.io/troubleshooting/error-messages.html
The reason this happens is that there are some PHP errors/warnings that show up before the PDF gets written, these make it a non-valid PDF therefore not readable by the Browser.
You can see it by downloading and opening the PDF file with a TextEditor and you'll see PHP errors instead of the standard %PDF-1.4 value (which should be the first value of the file).
If you suppress warnings error_reporting(E_ERROR | E_PARSE); you should be good to go.
Try this
$mpdf->Output('', "I");
so then file should be displayed inline in browser instead of downloading it.
if you want to download use "D" insted of "I"
I had the same problem, I didn't notice that header and footer (header was the problem) are still included in my index file. When I called my generate_pdf file it was pulling header and that created error. After I removed header pdf was generated successfully.

(Inline) PHP in domPDF 7.0

I switched from TCPDF to domPDF because it seems more convenient to handle when creating invoices from html to pdf (I am rather a low pro on PHP :)). Now that I created the html file as a PDF file I recognized it does not output any PHP in the PDF - since the data from my sql databanks should fill the PDF it is kinda a problem.
I saw that you can enable PHP in the options.php included in the src-folder and I tried to do like it is written in the manual (and also tried various other code lines) but it just doesn't want to work:
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
require_once ("$root/../xxx/dompdf/autoload.inc.php");
use Dompdf\Dompdf;
use Dompdf\Options;
$options = new Options();
$options->setIsPhpEnabled('true');
$dompdf = new Dompdf($options);
$dompdf->loadHtml(file_get_contents("testdomhtml.php"));
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream("bla",array("Attachment"=>0));
The PDF is shown but without the input from any PHP code.
If someone would be so kind, I would also be interested in knowing why and in how far enabling PHP is a security risk since I actually want to use that for my business. Would it be more advisable to wrap it all up in the main php file without loading external html and css files?
Thanks a lot in advance!
You could do something like this (not tested the code). Replace
$dompdf->loadHtml(file_get_contents("testdomhtml.php"));
With
ob_start();
include 'testdomhtml.php';
$output = ob_get_clean();
$dompdf->loadHtml($output);
More options How to execute and get content of a .php file in a variable?
Your file_get_contents("testdomhtml.php") will get actual content of file and will not execute any code inside it. Instead make it web accessible and pass URL to this page:
$dompdf->load_html_file('http://yourdomain.ext/testdomhtml.php');

Output buffering alternative php

I'm trying to save the content of a file into a PDF using html2pdf, but the file has some PHP codes which need to be processed. I made some research and I found out that I had to use output buffering so that the PHP content in the file can be processed. So I did something like:
<?php
require_once('html2pdf.class.php');
ob_start();
require_once('my_file.php');
$content = ob_get_clean();
// force download of $content to a PDF
$html2pdf = new HTML2PDF('P','A3','fr', false, 'ISO-8859-1');
$html2pdf->writeHTML($content);
$html2pdf->Output('file_name.pdf', 'D');
?>
The file my_file.php is the file that has some PHP code and the HTML content that I wanna save to a PDF, and the variable $content is the actual content with the PHP codes processed and everything. This works fine on Apache, but not on IIS.
Does anybody know an alternative way to make this work witout using ouput buffering? I tried file_get_contents('my_file.php'); but my php contents in my_file.php do not get processed when I do so.
Please, I'm looking for ways to do this without output buffering so that it can work on any server. I'm not looking for answers telling me to change my IIS server configuration or to use something else other than html2pdf.
Thanks in advance for any help
If you can modify the contents of my_file.php, you can put all the text into a variable there instead of outputting it directly.
You can use PHP/PDF Library http://php.net/manual/en/book.pdf.php
And follow this example : http://php.net/manual/en/pdf.examples-basic.php
Hope that helps :)
The easiest approach would be to edit my_file.php so that rather than containing HTML it assigns the HTML content to a PHP variable. Then all you need to do is echo the variable.
//other PHP processing goes here, or anywhere else.
$someVar = "hello world";
$myHTML = "<html>My output: $someVar </html>";
echo $myHTML;
It's an ugly way of handling HTML output, and I'm not saying it's good programming, but if you want to avoid editing config files it would be quick and easy.

converting the content of a php page to html page

I want to convert the content of a php file (this file is generated using some query from mysql database along with some image)to html file in order to create a pdf format. I tried converting php file to pdf but could not be succeeded. Kindly help with very short example as I am very new to the area.Thanks in advance.
you can generate a static html file from a url (to the php page) like so:
file_put_contents('static.html', file_get_contents('http://example.com/dynamic.php'));
Though writing the file to disk is probably unnecessary.
Probably your pdf function takes an html sting, in which case include and output buffering might be a suitable solution:
ob_start();
include 'dynamic.php';
$html = ob_get_clean();
create_pdf('mypdf.php', $html);
For a more specific example you would need to show your current code

Parsing XML file through PHP while using XSLT as the main template file

I have a lots (500ish) xml files from An old ASP and VBscript that was running on an old windows server. The user could click a link to download the requested xml file, or click a link to view how the xml file will look, once its imported into their system...
If clicked to view the output, this opened a popup window were the xml filename is passed via URL & using the xslt template file this would display the output.
example url = /transform.php?action=transform&xmlProtocol=AC_Audiology.xml
Now were using PHP5 im trying to get something that resembles the same output.
we started looking into xslt_create(); but this is an old function from php4
I'm looking for the best method to deploy this.
The main php page should check & capture the $_GET['xmlProtocol'] value.
pass this to the xslt template page as data;
were it will be output in html.
a general point in the right direction would be great!
You can find the documentation (+examples) of the "new" XSL(T) extension at http://docs.php.net/xsl.
php
// Transform.php
if(isset($_GET['action']) && $_GET['action'] == 'transform') {
// obviously you would never trust the input and would validate first
$xml_file = AFunctionValidateAndGetPathToFile($_GET['xmlProtocol']);
// Load up the XML File
$xmlDoc = new DOMDocument;
$xmlDoc->load($xml_file);
// Load up the XSL file
$xslDoc = new DomDocument;
$xslDoc->load("xsl_template_file.xsl");
$xsl = new XSLTProcessor;
$xsl->importStyleSheet($xslDoc);
// apply the transformation
echo $xsl->transformToXml($xmlDoc);
}
I had a similar problem about two years ago. I was using PHP5 but needed to use xslt_create(); or an equivalent. Ultimately, I switched to PHP4.
You can probably set your server to use PHP5 everywhere except for files in a certain folder. I believe that's what I did so I could process XSL files using PHP4 but the majority of the site still used PHP5.
It's possible that things have changed in the last two years and PHP5 has better support for something like xslt_create(); ---- I haven't been following recent changes.
Hope this helps!

Categories