First, thank you for the awesome module, saving tons of time for me.

I was able to alter the module code to allow for using webform field values in the pdf filename output, but am wondering if this could be a feature? Or, if someone could let me know if there is a better, smarter, non-module hackish way to do it.

I found this line in webform2pdf.module around line 1775:


 $filename = "webform_submission-". $node->nid .'-'. $submission->sid .".pdf";

I basically added an if/else conditional to check for a nid and then used cid's from the individual webform nodes like so:


 $filename = $submission->data[3]['value'][0].'_'.$submission->data[4]['value'][0].".pdf";

Is there a better way to achieve these results?

CommentFileSizeAuthor
#2 webform2pdf-894644-1.patch2.86 KBmr.york

Comments

shafter’s picture

Version: 6.x-1.1 » 6.x-2.x-dev

We are planning to enable rewriting filename through theming system in 2.x

mr.york’s picture

Status: Active » Needs review
StatusFileSize
new2.86 KB

I created theme_webform2pdf_filename theme function, please try this patch and use this function to modify filename.

mr.york’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

petr illek’s picture

How can I use this function? And where? Does exist some ready snippet I could use?

Thank you a lot for your work.

mr.york’s picture

Version: 6.x-2.x-dev » 7.x-3.x-dev
Status: Closed (fixed) » Active

.

roginald’s picture

You need to override the supplied theme function in your template.php, at least that is the route i chose. Here is an example that assumes your theme name is 'myexample':


function myexample_webform2pdf_filename($node, $submission) {
  $sid = is_object($submission) ? $submission->sid : $submission;
  if($node->nid == 342) {
    $pdf_file_name = $submission->data[2]['value'][0].'_'.$submission->data[1]['value'][0].".pdf";
  }
  else {
  $pdf_file_name = "webform_submission-" . $node->nid;
  $pdf_file_name .= !empty($sid) ? '-' . $sid : '';
  $pdf_file_name .= ".pdf";
  }
  return $pdf_file_name;
}

Here I checked the node id to get the form I wanted to affect, '342'. Then, for that webform only, I adjusted the pdf filename to use the values of the fields with id of 2 and 1. These two corresponded with last/first name fields in my form. You can use the devel module so traverse the node object and find whatever id values you need, if you want to use form values in the generated pdf file name, which I did.

So for me this generated a filename with the format: LAST_FIRST.pdf

Thank you to the module developers for making this module and adding this great feature. Saved me tons of time.

BenK’s picture

Subscribing

shafter’s picture

Status: Active » Fixed

Thanks for the resolution, roginald.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

stanborodyansky’s picture

hey so i am trying to change the file name for the pdf from webform2pdf. i did what you wrote, but its not working and is displaying some error messages. is there anything i could be doing wrong? i am using drupal 7...

roginald’s picture

Issue summary: View changes

update here for drupal 7. make sure to place in the correct template.php file for whatever theme is being used to generate the pdf, in my case i am using the 'seven' administration theme.


function myexample_webform2pdf_filename($vars) {
  $sid = is_object($vars['submission']) ? $vars['submission']->sid : $vars['submission'];
  if($vars['node']->nid == 21376) {
    $pdf_file_name = $vars['submission']->data[3]['value'][0].'_'.$vars['submission']->data[2]['value'][0].".pdf";
  } else {
  $pdf_file_name = "webform_submission-" . $vars['node']->nid;
  $pdf_file_name .= !empty($sid) ? '-' . $sid : '';
  $pdf_file_name .= ".pdf";
  }
  return $pdf_file_name;
}

HarlsHicks’s picture

@roginald,

I'm doing the same thing, and it works fine when you go to download the PDF. However, when the PDF is sent with the email generated by webform, the values don't display.

For Example,

function seven_webform2pdf_filename($vars) {
    $lname = $vars['submission']->data[5][0];
    $fname = $vars['submission']->data[6][0];
    $date = $vars['submission']->data[2][0];
    $position = $vars['submission']->data[1][0];
    $pdf_file_name = $lname . '_' . $fname . '_' . $date . '_' . $position . '.pdf';
    return $pdf_file_name;
}

When you download the PDF, this will generate correctly (i.e. Martin_James_3-12-15_General Manager.pdf).

However when the email comes through, the attached pdf has only underscores (i.e. ___.pdf).

I'm not really sure what the issue is unfortunately. If I don't override this function, then the default works perfectly fine. Any ideas?

joe huggans’s picture

In my case I only required that all attachments be named the same thing so my code was very simple, thanks for posting this function roginald

function MY_THEME_NAME_webform2pdf_filename($vars) {
  
  $pdf_file_name = "desired_filename.pdf";
 
  return $pdf_file_name;
}
HarlsHicks’s picture

I failed to mention that I figured out my issue in #13. It was another issue entirely that I opened here: https://www.drupal.org/node/2470353

tonyrboies’s picture

I'm trying to use @roginald's #12 code, but the line

$pdf_file_name = $vars['submission']->data[3]['value'][0].'_'.$vars['submission']->data[2]['value'][0].".pdf";

is generating error message: "Notice: Trying to get property of non-object in..."

Am using a newer version of the module (7.x-4.0), has anyone been able to change the pdf file name based on a user-submitted form value in this version, and if so, how?

Thanks, Tony

tonyrboies’s picture

Figured it out, hopefully this helps someone in the same boat.

function theme_webform2pdf_filename($vars) {
  $sid = is_object($vars['submission']) ? $vars['submission']->sid : $vars['submission'];
 
  if($vars['node']->nid == 14) {
  
  	// CUSTOM NAMING - ADD FORM FIELD 4 VALUE TO PDF FILENAME  
  	$submission = webform_get_submission($vars['node']->nid, $sid);
	$pdf_file_name = "my-custom-name-here-" . $submission->data[4][0] . ".pdf";

  } else {

	// BUILD PDF FILENAME DEFAULT METHOD
  	$pdf_file_name = "webform_submission-" . $vars['node']->nid;
  	$pdf_file_name .= !empty($sid) ? '-' . $sid : '';
  	$pdf_file_name .= ".pdf";
  
  }
  
  return $pdf_file_name;
}
scott_earnest’s picture

We ran into an issue where if the custom filename exceeded 34 characters (excluding the ".pdf") the attachment would be renamed to "Untitled attachment [sid].pdf". I also noticed that you can leave off the ".pdf" from this theme function it will automatically get added to the filename, allowing you up to 38 characters. Not sure where the .pdf extension would get automatically added to the filename so just to be safe we included it in the custom filename.

tonovcp’s picture

@roginald and @tonyrboies

Thank you, I got it working perfectly