Reputation: 1896
How can I make dompdf work with a Symfony 2.1 project? I followed this TUTORIAL but I'm still getting this exception:
Notice: Use of undefined constant DOMPDF_LIB_DIR - assumed 'DOMPDF_LIB_DIR' in [blablabla]/vendor/dino/dompdf/lib/DOMPDF/stylesheet.cls.php line 16
if not this, what is the best and simplest library to create pdf from html with Symfony 2.1 ?
Upvotes: 0
Views: 3705
Reputation: 6258
There's actually a must simpler way to include DomPDF in a Symfony2 project.
First, add DomPDF in your composer.json
file:
{
"require": {
// ...
"dompdf/dompdf": "dev-master"
}
}
After installation, edit the app/autoload.php
file of your Symfony application and add these lines at the end (before the return
statement) :
// Requirements for DOMPDF
define('DOMPDF_ENABLE_AUTOLOAD', false);
require_once __DIR__.'/../vendor/dompdf/dompdf/dompdf_config.inc.php';
And you're done!
But as Slavik mentioned above, there may be better solutions to dompdf, such as the KnpSnappyBundle which uses wkhtmltopdf
.
Upvotes: 1
Reputation: 3069
There is a much easier way, as dompdf itself now has rudimentary composer support: http://blog.viison.com/post/58696419659/dompdf-symfony2
Sorry for not posting the steps inline here but I've already written the blog post some time ago!
Upvotes: 2
Reputation: 636
There exist other options, for ex.:
wkhtmltopdf - a console html2pdf conversion tool that is based on WebKit engine, which guarantees standards-compliancy.
KnpSnappyBundle - bundle that integrates Symfony2 with wkhtmltopdf
Upvotes: 0
Reputation: 636
Here is what I've done to make DOMPDF available to me in a Symfony2 project:
require the DinoPDF composer package: php composer.phar require dino/dompdf, version 0.1.1 (or dev-master)
DomPDF init files need to be required() somewhere in the loading process:
add in your app/autoload.php:
require_once __DIR__.'/dompdf_config.inc.php';
dompdf_config.inc.php defines the paths to your DOMpdf installation. DinoPDF distribution changed the paths within the library, so this has to be taken care of:
DOMPDF_INC_DIR: Main DOMPdf classes. In dinoPDF distribution of DOMPdf this folder is renamed from dompdf/include to dompdf/lib/DOMPDF
DOMPDF_LIB_DIR: Additional resources - fonts, etc. In dinoPDF distribution of DOMPdf this folder is renamed from dompdf/lib to dompdf/lib/vendor
Here are the changes I've introduced to dompdf_config.inc.php:
/**
* The root of your DOMPDF installation
*/
define("DOMPDF_DIR", str_replace(DIRECTORY_SEPARATOR, '/', realpath(__DIR__.'/../vendor/dino/dompdf/')));
/**
* The location of the DOMPDF include directory
* Main DOMPdf classes
* In dinoPDF distribution of DOMPdf this folder is renamed from dompdf/include to dompdf/lib/DOMPDF
*/
define("DOMPDF_INC_DIR", DOMPDF_DIR . "/lib/DOMPDF");
/**
* The location of the DOMPDF lib directory
* Additional resources - fonts, etc.
* In dinoPDF distribution of DOMPdf this folder is renamed from dompdf/lib to dompdf/lib/vendor
*/
define("DOMPDF_LIB_DIR", DOMPDF_DIR . "/lib/vendor");
/** Include the custom config file if it exists */
if ( file_exists(__DIR__ . "/dompdf_config.custom.inc.php") ){
require_once(__DIR__ . "/dompdf_config.custom.inc.php");
}
def("DOMPDF_FONT_DIR", DOMPDF_LIB_DIR . "/fonts/");
This should correctly include the library, so that it could be used in controllers, for ex. like this:
/**
* @Route("/export_pdf", name="export_pdf")
*/
public function exportPDFAction($id)
{
$html =
'<html><body>'.
'<p>Put your html here, or generate it with your favourite '.
'templating system.</p>'.
'</body></html>';
$dompdf = new \DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$response = new Response();
$response->setContent($dompdf->output());
$response->setStatusCode(200);
$response->headers->set('Content-Type', 'application/pdf');
return $response;
}
Upvotes: 3
Reputation: 140205
You need to include the dompdf_config.inc.php
file too.
Upvotes: 0