Hi all,
As and other people whou use this cool module I think possibility display download link for download PDF file after submitting the form will be great faeture!

So after several hours of thinking I made small hack ;-)
I attach my version to this post

Unfortunately I not use GIt so cannot attach diff file.
I try write my hack code below.

This code add possibility to display the download link only if in "Form settings" for redirect selected as "Confirmation page".

So start from webform2pdf.install
I have no idea how to make upgrade for database, so after all manipulation will be need full reinstall.

##
# in function webform2pdf_install() in line 22 I add
##
'pdf_download_show'=> 0, 'pdf_download_title' => 'Download PDF',

##
# in function webform2pdf_schema() in line about 240 added
##
'pdf_download_show'=>array(
        'description' => 'Whether display the download link.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
), 
      
'pdf_download_title' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => 'Download PDF',
        'description' => 'Title for download link.',
),

 

Code for webform2pdf.settings.inc file

##
# in function webform2pdf_admin_settings($form, &$form_state) in line about 27 added
##

// download settings
$form['base']['pdf_download_show'] = array(
        '#type' => 'checkbox',
        '#title' => t('Display download link.'),
        '#description' => t('Display download link for download PDF file after form submit. Possible only if in "Form settings" selected "Confirmation page" for redirect'),
        '#default_value' => $default['pdf_download_show'],
);
$form['base']['pdf_download_title'] = array(
     '#type' => 'textfield',
     '#title' => t('Title for download link.'),
     '#default_value' => $default['pdf_download_title'],
     '#maxlength' => 255,
);
### in function webform2pdf_edit_form($form, &$form_state, $node) in line about 580
#   after:

$form['base']['page_orientation'] = array(
    '#type' => 'radios',
    '#title' => t('Page orientation'),
    '#default_value' => $default['page_orientation'],
    '#options' => array(
      'P' => t('Portrait'),
      'L' => t('Landscape'),
    ),
  );
##
# I added
##
  // download settings
$form['download'] = array(
      '#type' => 'fieldset',
      '#title' => t('User download settings'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#states' => array(
        'visible' => array(
          'input[name="enabled"]' => array('checked' => TRUE),
  		),
  	),
);
$form['download']['pdf_download_show'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display download link.'),
      '#description' => t('Display download link for download PDF file after form submit. Possible only if in "Form settings" selected "Confirmation page" for redirect'),
      '#default_value' => $default['pdf_download_show'],
  );
  $form['download']['pdf_download_title'] = array(
  	    '#type' => 'textfield',
  	    '#title' => t('Title for download link.'),
  	    '#default_value' => $default['pdf_download_title'],
  	    '#maxlength' => 255,
);
##
# in function webform2pdf_edit_form_submit($form, &$form_state)
##
# after (line about 1050)
$save['p_body'] = $form_state['values']['p_body']['value'];
$save['format'] = $form_state['values']['p_body']['format'];

# added
$save['pdf_download_show'] = $form_state['values']['pdf_download_show'];
$save['pdf_download_title'] = $form_state['values']['pdf_download_title'];

# in db_insert('webform2pdf') (line about 1100) added
'pdf_download_show' => $save['pdf_download_show'],
'pdf_download_title' => $save['pdf_download_title'],

# after (line about 1150)
$save['p_body'] = $webform2pdf_default['p_body'];
$save['format'] = $webform2pdf_default['format'];

# I added
$save['pdf_download_show'] = $webform2pdf_default['pdf_download_show'];
$save['pdf_download_title'] = $webform2pdf_default['pdf_download_title'];

# in db_update('webform2pdf') (line ~ 1180) added
'pdf_download_show' => $save['pdf_download_show'],
'pdf_download_title' => $save['pdf_download_title'],

Now go to file webform2pdf.download.inc

###
# in end added
###
/**
 * frontend download callback
 */
function webform2pdf_submission_download_pdf_site($node, $sid){
	if (!is_numeric($sid)) {drupal_not_found();}
	//check access to own
	if(!empty($_SESSION['webform2pdf'][ip_address()]['sids']) && in_array($sid,$_SESSION['webform2pdf'][ip_address()]['sids']) ){
		if($submission = webform_get_submission($node->nid, $sid)){
			return webform2pdf_submission_download_pdf($node, $submission,'I');
		}
	}
	drupal_access_denied();
}

And last changes in file webform2pdf.module

###
#in function webform2pdf_menu() add next:
##
$items['webform2pdf/%webform_menu/%/download'] = array(
    'title' => 'Download PDF',
    'page callback' => 'webform2pdf_submission_download_pdf_site',
    'page arguments' => array(1, 2),
    'access callback' => 'node_access',
    'access arguments' => array('view', 1),
    'file' => 'includes/webform2pdf.download.inc',
);

###
# in function theme_webform2pdf_content($vars) change this:
##
$p_body = _webform_filter_values($vars['template']['p_body'], $vars['node'], $vars['submission'], $email, false);

# to this
$p_body = _webform_filter_values($vars['template']['p_body'], $vars['node'], $vars['submission'], $email, false, true);

###
# and in end of the file added
###
/**
 * hook_form_FORM_ID_alter()
 */
function webform2pdf_form_webform_client_form_alter(&$form, &$form_state) {
	//check whether we need show link
	$webform2pdf = _webform2pdf_get_template($form['#node']->nid);
	if ($webform2pdf['pdf_download_show']){
		//attach submit function to form
		$form['#submit'][] = 'webform2pdf_client_form_submit';
		//save setting in to $form_state
		//$form_state['webform2pdf'] = $webform2pdf;
	}
	
}
/**
 * function called when webform_client_form submited
 * look: webform2pdf_form_webform_client_form_alter()
 */
function webform2pdf_client_form_submit(&$form, &$form_state){
	if ($form_state['webform_completed'] && !empty($form_state['values']['details']['sid'])){
		drupal_session_start();
		$_SESSION['webform2pdf'][ip_address()]['sids'][] = $form_state['values']['details']['sid'];
	}
}
function webform2pdf_preprocess_webform_confirmation(&$vars) {
	
	//check whether we need show link
	$webform2pdf = _webform2pdf_get_template($vars['node']->nid);
	if ($webform2pdf['pdf_download_show']){
		//build link
		//$path = webform2pdf_url_encode($vars['node']->nid, $vars['sid'], false);
		$path = 'webform2pdf/'.$vars['node']->nid.'/'.$vars['sid'].'/download';
		$title = empty($webform2pdf['pdf_download_title'])?t('Download PDF'):$webform2pdf['pdf_download_title'];
		$link = '<div class="download-link">'.l($title, $path, array( 'attributes' => array('target'=>'_blank','class' =>array('download-pdf')) )).'</div>';
		//add link to message
		$vars['confirmation_message'] = !empty($vars['confirmation_message']) ? $vars['confirmation_message'].$link : $link;
		
	}

}

That`s all, i hope it helps for someone ;)

Comments

technikh’s picture

+1 subscribing..

technikh’s picture

I followed the steps in this issue http://drupal.org/node/525682#comment-2013998
that was sufficient for my case.
your method looks interesting. I will try that when I get a chance.

mr.york’s picture

Status: Active » Needs work
StatusFileSize
new8.45 KB

1. I'd be great, if you'd use drupal coding standards
2. Please submit a patch of your modifications. Git instructions: http://drupal.org/project/webform2pdf/git-instructions
3. You are checking ip address to decide, which submission can be accessed, this is not safe enough.
4. What happens if the user wants to download the submission on an other computer?

fedik’s picture

I still study the git and drupal coding standards ;) ... sorry for that
I thought checking IP it will enough, because there also session checking ... and each user have access only for own session ... or I am wrong?
And user have no access for download on the another computer ... I made only for the "one time" download.
Ah, and I thought only about a anonymous users :)

shafter’s picture

It is an interesting feature, please create a separate module for that, webform2pdf module doesn't want to by-pass drupal built-in authorization methods.

fedik’s picture

StatusFileSize
new17.05 KB

ok, forget all that I wrote ... hehe :)
I have other solution, and hope it will be better
This patch add:
- allow user-end download by role (limited download for anonymous)
- show all downloads on the profile page for registered users
- display download link with confirmation message and do nothing if external redirect

fedik’s picture

Version: 7.x-3.0-rc2 » 7.x-3.x-dev
fedik’s picture

made more nicely patch (I hope) :)

fedik’s picture

Status: Needs work » Needs review
jaroslaw.kaminski’s picture

almost great but I got error:
Call to undefined function webform_get_submission() in /www/drupal/sites/all/modules/webform2pdf/webform2pdf.module on line 372,
and I added at 371 line:

module_load_include('inc','webform','includes/webform.submissions');
match79’s picture

It'd be great to see this patch reviewed and incorporated as this functionality would be very useful!

In the meantime, it's a bit of a hack, but the following can be set as the Custom URL for confirmation in the Webform Settings tab:

node/%nid/submission/%sid/downloadpdf

This will redirect to the generated PDF after the submit button is pressed - however since it points directly to the file, it will leave te completed form displayed in the browser window.

It looks like this same hack would be a better option in the 4.x release, since it allows tokens in the confirmation message: https://drupal.org/node/828566

EDIT: Tested and confirmed with webform 4.x, using the slightly different syntax to insert a link to the document into the confirmation message:

<a href="submission/[submission:sid]/downloadpdf">Submitted PDF</a>

summit’s picture

Hi with #10 no errors anymore? Can this be implemented now?
Greetings, Martijn