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?
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | webform2pdf-894644-1.patch | 2.86 KB | mr.york |
Comments
Comment #1
shafter commentedWe are planning to enable rewriting filename through theming system in 2.x
Comment #2
mr.york commentedI created theme_webform2pdf_filename theme function, please try this patch and use this function to modify filename.
Comment #3
mr.york commentedComment #5
petr illekHow can I use this function? And where? Does exist some ready snippet I could use?
Thank you a lot for your work.
Comment #6
mr.york commented.
Comment #7
roginald commentedYou 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':
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.
Comment #8
BenK commentedSubscribing
Comment #9
shafter commentedThanks for the resolution, roginald.
Comment #11
stanborodyansky commentedhey 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...
Comment #12
roginald commentedupdate 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.
Comment #13
HarlsHicks commented@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,
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?
Comment #14
joe huggansIn 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
Comment #15
HarlsHicks commentedI 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
Comment #16
tonyrboies commentedI'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
Comment #17
tonyrboies commentedFigured it out, hopefully this helps someone in the same boat.
Comment #18
scott_earnest commentedWe 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.
Comment #19
tonovcp commented@roginald and @tonyrboies
Thank you, I got it working perfectly