1

I was searching a way to save a layout of my custom component as PDF.

I tried various libraries JS / PHP. What I wanted was utf-8 support, tables, images to be properly saved in the pdf file.

I spent the last day trying to integrate the DomPDF library to my component. I read numerous guides and howtos with no luck.

The major problem was that Joomla changed the Document Renderer library after version 3.8 (I am not 100% percent about the version) and the latest version of DomPDF which no longer requires the config file [config.inc.php].

In previous Joomla versions the following guide should work fine : Creating PDF views

So using the DomPDF in the default view, was a problem for me, because I wanted the option to have a link to download as PDF from my List view and from my Item view.

Finally I did it using a raw view and a new layout file for clean output.

I am open to any corrections, suggestions or any other improvement.

Thank you

1
  1. Download the latest release of the DomPdf

  2. Inside Libraries folder place the a copy of dompdf folder from the release.

  3. Create a copy of the view file /components/mycom/views/item/view.html.php to /components/mycom/views/item/view.raw.php

Remove any scripts scripts and everything else that is useless in the the raw view. This view will be only a save as dialog to download the pdf file.

  1. Create a new layout in the /components/mycom/views/item/tmpl folder named pdf.php with the following code :

The file will not have nothing to display so we will save all the output to a php variable.

defined('_JEXEC') or die('Restricted access');
require_once JPATH_LIBRARIES  . '/dompdf/autoload.inc.php';
use Dompdf\Dompdf;


$html ='<style type="text/css">';
$html .='/*place your css if any here*/';
$html .='</style>';

$html .='<h1>' . $this->item->title . '</h1>';
$html .='<div>';
$html .= $this->item->content; //or any other field you like
$html .='</div>';

$dompdf = new Dompdf();
$dompdf->set_option('isHtml5ParserEnabled', true);
$dompdf->setPaper('A4', 'portrait');
$dompdf->setBasePath(JPATH_LIBRARIES . '/dompdf');
$dompdf->loadHtml($html);
$dompdf->render();
$dompdf->output();

$filename = $this->item->alias . '.pdf';

$dompdf->stream($filename);

Then just place a link anywhere in your /components/mycom/views/item/tmpl/default.php layout file using raw type pointing to the new layout pdf.php

<a href="index.php?option=com_mycom&view=myview&type=raw&layout=pdf&id=<?php echo $this->item->id?>" target="_blank">Download PDF</a>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.