Skip to main content

Convert Joomla Content to PDF

C2PDF is a Joomla plugin. This component allows you to create PDF documents based on your Joomla CMS. There is a module to display a 'Download PDF' link. 

For an example and a demo see the bottom of this page.

It uses the component template (?tmpl=component) to get a simplified version of a page. Then C2PDF then uses the Dompdf library for creating PDF documents from the HTML.

It should work with any Joomla component. 

The plugin & module have a small footprint of about 10 Kilobytes of code. The dompdf library is quite large, about 10 Megabytes. However, dompdf is only loaded when needed.

Installation & Setup

Installation is as with any other package. Download the zip file and upload it or use the download link directly. 

Ensure the plugin is enabled. 

(Non) Persistent content

To generate the PDF the plugin performs an extra GET request to the current page. Therefor, the information on a page must be persistent between requests. 

If you have POST request, for example after a form submission the information is not persistent. For these pages the plugin allows to use a section of the current page. See the demo on this page.

More in this document.

Restrictions

  • The cachedir must be writable, Joomla's FTP layer is not supported
  • The server must allow HTTP request, including loopbacks.
  • Javascript won't work. So sliders, maps, social feeds and similar may not display. Content hidden of changed by javascript won't work.
  • Dompdf is (mostly) CSS 2.1 compliant, so if your layout relies on modern CSS the layout will break. Fix it using extra CSS. See below.

Web bots & spiders

Most likely you don't want web spiders indexing your PDF's, it's duplicate content with your HTML page. The module and plugin have a few features to prevent indexing:

  • Send a noindex header for the generated PDF (plugin option)
  • Make the PDF link nofollow (all module layouts)
  • Check for a valid session (plugin & module option)
  • hide/cloak the PDF link. (Cloaking layout in the module)

Fonts

There are a few restrictions on the fonts. See the dompdf-wiki for details. You can set a fall-back font in the plugin settings, or define a font and @font-face in your injected CSS.

Adding CSS

You can inject extra CSS using a file, see the Plugin setting. 

Adding header and footer sections

You can add a header and/or footer by using a different site template (or changing the component.php itself). See the module option: Use site template.

Module

The module display a simple link to generate a PDF. 

Current page PDF

See Non-persisitent content above.

The content field should contain a CSS selector or the HTML-element to be converted to PDF.

The title field may contain a CSS selector of the HTML element used as title for the PDF.

The menu field may point to a different menu item to be used as base for the PDF. 

How it works

On clicking the button/link the selected HTML-elements are extracted from the webpage and saved in the users' session. After that, the actual download PDF link is called, the plugin replaces the component output and the title with the saved HTML-elements.

To work correctly, you must use the section layout or an override using _section

Plugin: Extra CSS File

This allows to inject a custom CSS file only used when generating a PDF page.

You can also add some CSS directly into your templates css files using @page and @media dompdf

@page {
    margin:3em 1em; /* sets the margin */ 
}
@media dompdf {
body {
color:blue
}

Plugin: Default Font

The PDF generator has a few default fonts. Use these as a fallback font. 

Plugin: Add Robots noindex tag

This adds a robots  noindex header tag to the server response.

Turn it off if  you want your PDF's indexed by google. 

(The link in the module has a nofollow, you might want to change that as well)

Plugin: Cache PDF's

Cache the generated PDF. The cache is based on the generated HTML within the component section, not assets like images and CSS files. 

PDF files can get quite large, especially with images. Cache files can be cleared using Maintenance →  Clear Cache from the System Dashboard.

If you change the content outside the component section like a CSS file (including the custom CSS file from the plugin settings) or change the font in the settings, you must clear the cache also to see the effect.

Generating the PDF is quite an operation, so caching is highly recommended

Module: Layout

This selects the layout for the module. Either from the module or a template override. 

There is a helper function to show the content only on article pages

use Brambring\Plugin\System\C2pdf\Extension\PluginActor as C2pdf;
if (C2pdf::isArticle()) {
?>
    <div id="<?= 'mod-c2pdf' . $module->id; ?>" class="mod-c2pdf c2pdf">
        <a rel="nofollow" target="_blank" href="/<?= $pdfLink ?>">DOWNLOAD PDF</a>
    </div>
<?php } ?>

Module: Anchor Text

This option changes the text display in the print link or button.

For more detailed customizations, create on override.

Module: Use site template

This controls which site template is used. Default is the component template (https://example.com/?tmpl=component). The file for the site template can be found in [ROOT]/template/<template>/component.php.

You can not override this file, however you can add your own for example: [ROOT]/template/<template>/print.php and change this option.

Be aware that a tool like Akeeba Admin Tools blocks unknown site templates. You can change this in the WAF configuration.

This option is useful if you want to add a custom header and/or footer to the generated PDF. Add the html directly or create module positions, for example:

<jdoc:include type="modules" name="print-top" style="none" />
<jdoc:include type="component" />
<jdoc:include type="modules" name="print-bottom" style="none" />

Another example with a logo, as used on this site, can be found in the assets' folder of the plugin.

Plugin & Module: Check Session

As the link in the module has a nofollow attribute, robots shouldn't follow that link. However, misbehavior does occur. Bingbot is quite notorious in ignoring robot settings. And bad bots usually do. Generating the PDF is quite an operation, and repeated requests for PDF's might impact the performance of a site.

This option prevents users without cookies (aka robots) from retrieving the PDF.

  • This might not work if your website uses (external) caching, preventing user sessions.
  • This options must be enabled or disabled in both the plugin and the module.

If a user session expires, the PDF link will not work anymore. It's probably better to turn this option off and use the 'Add Robots noindex tag' option with link cloaking.

Attribution link

Feel free to remove the attribution link from the module layout by creating an override.

Outdated

Using PHP or JavaScript to generate PDF files is a kind of outdated. Developers op DomPDF en mPDF can't keep up with the changing CSS rules and haven't even started supporting JavaScript. 

Hitting CTRL-p or CMD-p by your visitors and using the browsers print function is probably the best method to get a decent print of your website. Add some @media print CSS to hide parts like navigation, and you are done. 

If you want to generate PDF's of a site in bulk, solutions with a headless browser would automate that task. 

Downloading web pages as PDF files can have several benefits. Here are some reasons why it can be useful:

  1. Portability: PDF files are platform-independent, meaning they can be opened and viewed on any device without changing the formatting. This makes it easy to share and view information across different devices.

  2. Preservation of formatting: PDF files preserve the formatting of the original web page, including fonts, images, and layout. This means that the content looks exactly the same as on the website, which can be useful for saving important information.

  3. Offline access: When you download a web page as a PDF file, you can save it and view it offline later. This is useful if you don’t have an internet connection or if you want to view the information at a later time.

  4. Easy to share: PDF files are easy to share via email, cloud storage, or other methods. This makes it easy to share information with others.

There are several ways to download web pages as PDF files, depending on the browser you are using. For example, in Google Chrome, you can save a web page as a PDF file by clicking on the three dots in the upper right corner of the browser window and then selecting “Print”. In the print menu, you can select “Save as PDF” as the printer and then click “Save” to save the web page as a PDF file.