Adam Hughes
Adam Hughes

Reputation: 11

Using dompdf in joomla component has incorrect content

I'm creating a new compnent in Joomla 3 that will use dompdf to generate PDF's of certain information on the screen.

I have tested my dompdf code outside of joomla and everything works okay, but as soon as I bring the code inside my component it goes wrong.

Dompdf generates the PDF, but it is invalid and does not open correctly.

I have opened the PDF in a text editor and the issue is that the pdf that dompdf is generating has gotg all the Joomla headers in (stylesheets, jaavascript etc.) even though I'm only passing some dedicated HTML though to it.

Is there anything that I'm missing ? The code is listed below

$fhtml = '<html><head><title>Sales Order</title>';
$fhtml .= PHP_EOL . '<link type="text/css" href="test.css" rel="stylesheet" /></head><body>';
$fhtml .= PHP_EOL . '<center><img src="elite.jpg" /><img src="premierdealer.jpg" /><img src="logo.jpg" /><img src="traveller.jpg" /><img src="childrens.jpg" /><img src="westfield.jpg" /></center>';
$fhtml .= PHP_EOL . '<h2>Header</h2><p>Para</p>';
$fhtml .= PHP_EOL . '</body></html>';

require_once 'dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->setPaper('A4', 'portrait');
$dompdf->loadHtml($fhtml);
$dompdf->render();
$dompdf->stream('salesorder');

You can see from the code above that I pass the html over to dompdf->loadhtml. A pdf is generated, but when I open teh PDf in a text editor (it is a corrupted PDF so I cannot open it with a pdf reader) I find the following inside the PDF :

<!DOCTYPE html>
<html lang="en-gb" dir="ltr">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta charset="utf-8" />
<base href="http://test1.questleisure.local/index.php/portal/portal-home" />
<meta name="keywords" content="quest leisure air awnings caravan awnings camping caravan air tents outdoor funriture" />
<meta name="rights" content="Quest Leisure" />
<meta name="description" content="Quest Leisure Public Website" />
<meta name="generator" content="Joomla! - Open Source Content Management" />
<title>Portal Home</title>
<link rel="stylesheet" href="/media/com_questportal/css/default.css" />
<link rel="stylesheet" href="/plugins/system/rokbox/assets/styles/rokbox.css" />
<link rel="stylesheet" href="/media/gantry5/assets/css/font-awesome.min.css?57344a42" />
<link rel="stylesheet" href="/media/gantry5/engines/nucleus/css-compiled/nucleus.css?57344a42" />
<link rel="stylesheet" href="/templates/rt_galatea/custom/css-compiled/galatea_11.css" />
<link rel="stylesheet" href="/media/gantry5/assets/css/bootstrap-gantry.css?57344a42" />
<link rel="stylesheet" href="/media/gantry5/engines/nucleus/css-compiled/joomla.css?57344a42" />
<link rel="stylesheet" href="/media/jui/css/icomoon.css" />
<link rel="stylesheet" href="/templates/rt_galatea/custom/css-compiled/galatea-joomla_11.css" />
<link rel="stylesheet" href="/templates/rt_galatea/custom/css-compiled/custom_11.css" />
<link rel="stylesheet" href="/templates/rt_galatea/css/demo.css" />
<link rel="stylesheet" href="/templates/rt_galatea/css/animate.css" />
<script src="/media/system/js/mootools-core.js"></script>
<script src="/media/system/js/core.js"></script>
<script src="/media/system/js/mootools-more.js"></script>
<script src="/plugins/system/rokbox/assets/js/rokbox.js"></script>
<script src="/templates/rt_galatea/js/jui/jquery.min.js"></script>
<script src="/media/jui/js/jquery-noconflict.js"></script>
<script src="/media/jui/js/jquery-migrate.min.js"></script>
<script src="/media/jui/js/bootstrap.min.js"></script>
<body class="gantry g-galatea-style site com_questportal view-questportal no-layout task-display dir-ltr itemid-229 outline-11 g-offcanvas-left g-default g-style-preset1">
    <div id="g-offsidebar-overlay"></div>
    <div id="g-offcanvas"  data-g-offcanvas-swipe="1" data-g-offcanvas-css3="1">
<div          
     class="g

So dompdf is getting all thebasic Joomla headers from the buffer, even though I'm only passing it certain html. Is there anything I have to do to stop dompdf using all the headers from joomla, and just use the HTML I pass it ?

Cheers

Upvotes: 0

Views: 463

Answers (1)

Adam Hughes
Adam Hughes

Reputation: 11

I have traced this issue back to the way that dompdf->stream() works. If I create a physical pdf file using dompdf->output() then the pdf file is 100% correct. If I then use dompdf->stream() afterwards the resulting PDF file has all the joomla headers and stylesheets etc, in which causes the issue. The code showing the difference is below :

$fhtml = '<html><head><title>Sales Order</title>';
$fhtml .= PHP_EOL . '<link type="text/css" href="dompdf.css" rel="stylesheet" /></head><body>';
$fhtml .= PHP_EOL . '<center><img src="elite.jpg" /><img src="premierdealer.jpg" /><img src="logo.jpg" /><img src="traveller.jpg" /><img src="childrens.jpg" /><img src="westfield.jpg" /></center>';
$fhtml .= PHP_EOL . 'Test content';
$fhtml .= PHP_EOL . '</body></html>';

require_once JPATH_COMPONENT . '/libraries/dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->setPaper('A4', 'portrait');
$dompdf->setBasePath(JPATH_COMPONENT . '/libraries/');
$dompdf->loadHtml($fhtml);
$dompdf->render();
$dompdf->output();

$file_to_save = JPATH_COMPONENT . '/temp/test.pdf';
file_put_contents($file_to_save, $dompdf->output());

$dompdf->stream();

In the example above the test.pdf file is 100% correct, but the downloaded file (created by the dompdf->stream() command) is actually corrupt and contains all the chrome from joomla. I'm closing this question off and will raise a new one with the issue between stream and output instead.

Upvotes: 0

Related Questions