3

My goal is to generate a PDF view of a content node. I've set up the Views PDF module with the TCPDF class, but performance is awful. Not just slow. I mean AWFUL. It's an aggravating experience when viewing a simple page of content as a PDF, and downright painful when viewing content that should be several hundred PDF pages long. Totally unacceptable.

Does anyone have a good solution for combatting this? All I'm looking to do is take a single node and offer a "view as PDF" link/functionality. It seems like this should be pretty straightforward, but I haven't been able to solve that yet.

Any help would be appreciated. Thanks!

2
  • In my experience PHP PDF tools are all very memory-hungry (tcpdf perhaps being the worst offender), so it makes sense that generating a few hundred pages would be a very painful process to sit through. I've yet to find a decent PHP solution for generating PDFs. Recently wrote one in java though using pdfbox and it's an absolute dream performance-wise. If you can possibly work out a solution that uses anything other than PHP to generate the PDF you'll be in a much better position. I don't know of any views modules with that sort of functionality, but you never know
    – Clive
    Sep 14, 2013 at 20:59
  • If you're not happy with the TCPDF result i.e the formatting doesn't look right, bad performance etc then I would recommend going with drupal.org/project/phpwkhtmltopdf. You will need to install wkhtmltopdf on server before you can use this module. wkhtmltopdf renders pdf through a web browser so you will get pdf looking the same way it looks on screen.
    – Ash U
    Mar 21, 2017 at 23:59

4 Answers 4

1

I had never had any success with the html>pdf generating tools for PHP coming out looking 1:1 without issues ranging from style to markup to server side error with the libraries and/or incompatibility with PHP versions, package errors and whatnot.

Another option is to use PhantomJS or an equivalent tool to use a headless browser to output a PDF file for you. Note though, that PhantomJS is possibly going to no longer be maintained, as Chrome now has a headless equivalent.

However, there is a module for Drupal 7 and 8 that provides this functionality (disclosure, I am a maintainer) but it may take a little elbow grease on your end to get it to work the way you'd like. A base js controller for PhantomJS is provided, but you can alter it. If all you need is an equivalent of doing File > Print to PDF in the browser, this will do it without as much hassle as all the PHP libs.

https://www.drupal.org/project/phantomjs_capture

As an alternative to that, there is a hosted service provider with an API you can use as well (disclosure - I have nothing to do with it) that might suit you.

1

From my experience, the kind of functionality "View as PDF" is better answered with a link "Print" or "Save" outputting the content with specific html styled for that purpose. Once I did what you are trying to do, but nowadays I changed my view.

0

If you want to generate a pdf file from a single node, I would recommend the PDF using mPDF module.

Hope it will help you.

1
  • mPDF is better but if the pages are too long it does end up taking a lot of resources. I would recommend going with drupal.org/project/phpwkhtmltopdf. You will need to install wkhtmltopdf installed on server before you can use this. This will ensure that your PDF file is rendered the same way it looks on screen.
    – Ash U
    Mar 21, 2017 at 23:57
0

You can implement dompdf library in your custom module. It is compliant HTML layout and rendering engine written in PHP. PDF rendering is provided either by PDFLib or by a bundled version the R&OS CPDF class.

The conversion is pretty straight forward. Here is the code example:

<?php
// Require the Composer autoloader or use Composer Manager module.
// @see: http://getcomposer.org/doc/00-intro.md
require 'vendor/autoload.php';

// Disable DOMPDF's internal autoloader if you are using Composer Manager module.
define('DOMPDF_ENABLE_AUTOLOAD', false);

// include DOMPDF's default configuration
require_once 'vendor/dompdf/dompdf/dompdf_config.inc.php';

$htmlString = ''; // Replace $htmlString with html of of your content node.
$dompdf = new DOMPDF();
$dompdf->load_html($htmlString);
$dompdf->render();
$dompdf->stream("sample.pdf");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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