(Inline) PHP in domPDF 7.0 - php

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

Related

Dompdf -> Is it possible to render a pdf without copy select functions

I generate a PDF with DOMPDF from my html design.
But i need to disable the copy + select function inside the pdf text.
I guess a jpg version of the html page would be a good solution inside the pdf but i'm not sure if this is possible.
Any idea or solution for rendering the pdf file non selectable (as a jpg maybe)
Below renders my A4 format html design:
$html = mb_convert_encoding($pdf_html, 'HTML-ENTITIES', 'UTF-8');
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream("dompdf_out.pdf", array("Attachment" => false));
Thanks
I have tested this answer and it works in Adobe Reader. The first two arguments are passwords, if you make them empty the document will open without a password.
However this copy prevention does not work, for instance, in the build-in PDF reader of Firefox. So reliable protection from copying doesn't seem possible.
However, now you know how you can password protect a file, and that could be a good solution as well? After all, if someone cannot access a PDF they cannot copy it.

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

Generate PDF from HTML/CSS/PHP in Symfony 1.4

I am working on a Symfony 1.4 project. I need to make a PDF download link for a (yet to be) generated voucher and I have to say, I am a bit confused. I already have the HTML/CSS for the voucher, I created the download button in the right view, but I don't know where to go from there.
Use Mpdf to create the pdf file
http://www.mpdf1.com/
+1 with wkhtmltopdf
I'd even recommand the snappy library.
If you use composer, you can even get the wkhtmltopdf binaries automatically
Having used wkhtmltopdf for a while I've moved off it as 1) it has some serious bugs and 2) ongoing development has slowed down. I moved over to PhantomJS which is proving to be much better in terms of functionality and effectiveness.
Once you've got something like wkhtmltopdf or PhantomJS on your machine you need to generate the HTML page and pass that along to it. I'll give you an example assuming you use PhantomJS.
Initially set what every request parameters you need to for the template.
$this->getRequest->setParamater([some parameter],[some value]);
Then call the function getPresentation() to generate the HTML from a template. This will return the resulting HTML for a specific module and action.
$html = sfContext::getInstance()->getController()->getPresentation([module],[action]);
You'll need to replace the relative CSS paths with a absolute CSS path in the HTML file. For example by running preg_replace.
$html_replaced = preg_replace('/"\/css/','"'.sfConfig('sf_web_dir').'/css',$html);
Now write the HTML page to file and convert to a PDF.
$fp = fopen('export.html','w+');
fwrite($fp,$html_replaced);
fclose($fp)
exec('/path/to/phantomjs/bin/phantomjs /path/to/phantomjs/examples/rasterize.js /path/to/export.html /path/to/export.pdf "A3");
Now send the PDF to the user:
$this->getResponse()->clearHttpHeaders();
$this->getResponse()->setHttpHeader('Content-Description','File Transfer');
$this->getResponse()->setHttpHeader('Cache-Control','public, must-revalidate, max-age=0');
$this->getResponse()->setHttpHeader('Pragma: public',true);
$this->getResponse()->setHttpHeader('Content-Transfer-Encoding','binary');
$this->getResponse()->setHttpHeader('Content-length',filesize('/path/to/export.pdf'));
$this->getResponse()->setContentType('application/pdf');
$this->getResponse()->setHttpHeader('Content-Disposition','attachment; filename=export.pdf');
$this->getResponse()->setContent(readfile('/path/to/export.pdf'));
$this->getResponse()->sendContent();
You do need to set the headers otherwise the browser does odd things. The filename for the generated HTML file and export should be unique to avoid the situation of two people generating PDF vouchers at the same time clashing. You can use something like sha1(time()) to add a randomised hash to a standard name e.g. 'export_'.sha1(time());
Use wkhtmltopdf, if possible. It is by far the best html2pdf converter a php coder can use.
And then do something like this (not tested, but should be pretty close):
public function executeGeneratePdf(sfWebRequest $request)
{
$this->getContext()->getResponse()->clearHttpHeaders();
$html = '*your html content*';
$pdf = new WKPDF();
$pdf->set_html($html);
$pdf->render();
$pdf->output(WKPDF::$PDF_EMBEDDED, 'whatever_name.pdf');
throw new sfStopException();
}

Page Not Redirecting to controller after generating a pdf file

I am using Codeigniter as a PHP framework and DOM PDF to generate pdf files. I have the following codes in my Controller.
// Some other codes
include_once('dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF();
$base_path = $_SERVER['DOCUMENT_ROOT'];
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("invoice_$studentid.pdf");
redirect("my_Controller");
The problem is after generating the pdf file it is not redirecting to the Controller. Could you please tell me how to solve this problem?
$dompdf->stream is sending the PDF to the browser. You can't also send a redirect header. You're trying to output two responses to one request, which is impossible.
This doesn't seem like it should be a problem. The browser will stay on whichever page the user was on when they clicked the link to download the PDF. If you really want them to be forced elsewhere (you probably don't, that's a very different user experience from how download links work everywhere else) you could do something with JavaScript.

Webpage convert to PDF button

I have a website now and I want to create a button on it to convert this page to PDF.
Is there any code to make this happen? I cannot find it on the internet.
So I want to have a button and when I press on it it converts the page to a .PDF file.
I do not want to use a third party website to generate the PDF's. I want to use it for internal purposes to generate files with PHP. So I need the code what can make a PDF for each page.
I use wkhtmltopdf - works very well - http://code.google.com/p/wkhtmltopdf/ there is a PHP wrapper
Updated based on comments below on usage :
How to use the integration class:
require_once('wkhtmltopdf/wkhtmltopdf.php'); // Ensure this path is correct !
$html = file_get_contents("http://www.google.com");
$pdf = new WKPDF();
$pdf->set_html($html);
$pdf->render();
$pdf->output(WKPDF::$PDF_EMBEDDED,'sample.pdf');
Use FPDF. It's a well-respected PDF-generating library for PHP that is written in pure PHP (so installing it should be dead simple for you).
Try this:
http://www.macronimous.com/resources/Converting_HTML2PDF_using_PHP.asp
It will convert HTML to a PDF using FPDF and HTML2PDF class.
Also found this:
http://www.phpclasses.org/package/3168-PHP-Generate-PDF-documents-from-HTML-pages.html

Categories