Drupal Webform 4.x Submission Download as PDF

I’ve been messing around a lot with custom displays, webforms and output lately with my departmental project.

As a final request they wanted each Webform Submission to be able to be downloaded as a PDF and not look poorly made.

Webform 3.x had a GREAT pdf preview by simply supplying a token of %email_values% to the PDF content using the module Webform2PDF.

Part way through my project I had to update to Webform 4.x (for Conditional form fields on the same page). In doing so the token %email_values% no longer exists; it does nothing. Webform 4.x uses a Drupal 7 Token based approach to token output. A webform value is selected via a token like [submission:values:student_name], the general structure is: [submission:FIELDSET:COMPONENT_FORM_KEY].

Well, let me tell you — I miss the good old days of %email_values% outputting a glorious full page PDF of a submission with all the questions and answers, answer grids and everything there! I am not writing 50 tokens to get all the values of a webform (that may change over time).

So I output a PDF, completely from code — and never use the built-in tokens in PDF generation. This was either an act of brilliance, or lunacy. When I render a PDF to the screen I add a download link to a PDF:

/**
 * Implements hook_webform_submission_render_alter().
 *
 * Add a download PDF link to the beginning of the submission view.
 */
function MYMODULE_webform_submission_render_alter(&$renderable) {
  // dpm($renderable);
  if (strpos(current_path(), 'open-applications') !== FALSE) {
    $nid = $renderable['#node']->nid;
    $sid = $renderable['#submission']->sid;
    $download_pdf_link = l('Download Pdf', "node/$nid/submission/$sid/downloadpdf");
    $renderable['reference_regarding']['#prefix'] = $download_pdf_link.'<br>';
  } else {
// DEBUG
//    $renderable['reference_regarding']['#prefix'] = '**' . substr(current_path(),-11) . '**';
  }
  // This glorious hack from: https://drupal.org/node/1597892
  //
  // and see:
  // http://drupal.stackexchange.com/questions/50086/how-to-use-submissionvalues-in-email-template-in-webforms

  if (substr(current_path(),-11) === 'downloadpdf') {
    foreach ($renderable as $i => $item) {
      if (is_array($item) && !empty($item['#title'])) {
        $renderable[$i]['#title'] .= "<br>";
      }
    }
  }
  unset($renderable['webform_rid']);
  unset($renderable['reference_email_address']);
}

I then insert the following code into the Page Body of the PDF content for the webform2pdf configuration settings:

pdf_config

<?php
// path is of the format: node/1/submission/1/downloadpdf1
$path = current_path();
$parts = explode('/', $path);
$sid = $parts[3];
$content = _MYMODULE_render_reference_submissions(array($sid));
return $content[0];
?>

Lastly, I have this module code that runs:

/**
 * This function builds an array of HTML fragments of rendered submissions from
 * a collection of Submission IDs.
 */
function _MYMODULE_render_reference_submissions($sids) {
  $content = array();
  if (count($sids)) {
    module_load_include('inc', 'webform', 'includes/webform.submissions');
    $webform_nid = 1;
    $webform_node = node_load($webform_nid);

    foreach ($sids as $sid) {
      $submission = webform_get_submission($webform_nid, $sid, TRUE);
      $webform_renderable = webform_submission_render($webform_node, $submission, 'test@test.com', 'html');
      $content[] = drupal_render($webform_renderable);
    }
  }
  return $content;
}

This code outputs the Submission as HTML, the webform2pdf module PDF library can interpret and embed simple HTML into the body of a PDF, sweet! It looks exactly like a web-preview of the survey!

Webform2PDF uses a 3rd party PHP library for PDF generation called TCPDF. For webform2pdf usage and TCPDF 6.x, I had to apply 2 patches to TCPDF look in the Drupal issue queue for webform2pdf for the fixes.

I can’t easily show the output of a PDF here because it would contain sensitive client data. But, all this code does work!

Looking for quality Drupal Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , | Posted under Drupal | RSS 2.0

Author Spotlight

David Gurba

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

3 Comments on Drupal Webform 4.x Submission Download as PDF

  1. Shnifti says:

    Hey Man,

    this looks great and could perhaps solve my webform2pdf issues: https://www.drupal.org/node/2348329
    in which files do you write your source code? e.g hook_webform_submission_render_alter().
    Is this your own module or do you hack it into webform2pdf?

    please explain more closely where to put the codes.

    • Shnifti says:

      I forgot:

      Thanks in advance and kind regards!

      • David Gurba David Gurba says:

        Hi. My code is a somewhat tricky way of using webform2PDF to render the webform submission as it would appear in basic HTML — but in a PDF. I am not altering how fields in the webform are displayed from the screen to the PDF.

        This is why I said in Webform 3.x the %email_values% token contained the rendered submission as I wanted, and webform 4.x no longer did:

        Webform 3.x had a GREAT pdf preview by simply supplying a token of %email_values% to the PDF content using the module Webform2PDF.

        Part way through my project I had to update to Webform 4.x (for Conditional form fields on the same page). In doing so the token %email_values% no longer exists; it does nothing. Webform 4.x uses a Drupal 7 Token based approach to token output. A webform value is selected via a token like [submission:values:student_name], the general structure is: [submission:FIELDSET:COMPONENT_FORM_KEY].

        Since you want to change how things look I recommend attempting to follow these directions for a webform 3.x custom PDF. A similar approach should work for webform 4.x.
        http://bigsnowball.com/content/output-drupal-webform-pdf

        I haven’t had to customize my webform pdf output so unfortunately I cannot give you an exact answer at this time.

Leave a Reply

Your email address will not be published. Required fields are marked *


× 2 = 10

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>