Set up automated, professional, formatted PDFs from Drupal webforms using Views and Rules - and have them emailed once created.
This tutorial shows how to create a high-resolution PDF file from a Drupal webform. The same basic concept can be used to create PDF files from nodes as well - you just won't need the Webform Rules module.
The 4 steps in summary are:
- Install the required modules and libraries
- Create a Webform
- Create a View to convert the Webform to a PDF.
- Create a Rule to save the PDF to the server, and email it if you want.
1. Requirements for this project:
- Have Drupal 7 and these modules installed: Views, Rules, Webform 4 (must be 4 for the tokens), Webform Rules, Views PDF , jQuery Update
- These libraries are needed for Views PDF: TCPDF Version 5 or greater, FPDI , FPDF_TPL . Here's my structure and versions:
- sites/all/libraries/tcpdf that contains the contents of tcpdf-6.0.020.zip
- sites/all/libraries/fpdi that contains the contents of FPDI-1.4.4.zip and FPDF_TPL-1.2.3.zip . For these, I recommend creating the fpdi directory first and putting the zips in there before unzipping - they both contain loose files.
- To have the PDF emailed automatically after it's created, you'll need Mimemail (and its Mimemail Action submodule) and Mailsystem
2. Create the Webform
Create a webform and add the fields that you need.
3. Create the View
Create a new view:
- On the first screen, choose Show Webform submissions
- Uncheck Create a page and Create a block
- Click Continue & Edit
Add a contextual filter so that each PDF shows only that webform's data:
- Under Contextual Filters in the right column (click the word "Advanced" if you don't see this), add a "Webform submissions: Sid" filter.
- Click Apply on the screen "Configure contextual filter" (The only configuration I use here is ' When the filter value is not in the URL: Show "Page not found"')
- Under Fields, click Add
- Use the FIlter to find Webform Submission Data:Value
- In the form that comes up, find the webform you wish to use via the Webform node field
- Once the Webform node is selected, a new box appears called Component data, which is a select list showing all the webform fields available on that webform
- Choose the first field you want to display, and the Display format you want for it
- Add the rest of the webform fields by repeating the above steps for each field
Also: Under pager, choose "display all items" and you may want to remove any Sort and Filter items.
Add the PDF display:
- Under Displays, add a PDF Page
- Under PDF Settings: Path, add a path (which is required, but we're not using it - we'll be setting the path via a Rule - so I believe you can add any path you want here. To be safe, a path like "/ sites/default/files/viewspdf/%/webform.pdf" will replace the percent symbol with each webform submission's ID, giving it a unique path.)
- Under PDF Settings: PDF Page Settings , configure the page size, orientation and margins
- UnderPDF Settings: PDF Fonts Settings , configure the font
- Under PDF Settings: PDF Template Settings, adding a background template with the upload button will store template files in /sites/default/files/views_pdf_templates. Once there, you can choose a template to be placed in different areas of your PDF. For this example, we'll upload a high-resolution PDF of 8.5x11 corporate letterhead.
- Now under Format: Show: PDF FIelds, click Settings.
- You'll see clickable sections for each of the webform fields you added.
- By default, they'll print sequentially and vertically. If you need any different placement, configure it here. (warning - you may need to do a lot of testing to get it right!)
- I used the background uploaded in the previous step by choosing it under Template PDF.
4. Create the Rule
- Add a new Rule that reacts on the event "After a webform has been submitted"
- Add a condition "Webform has name"
- Value for THE FORM ID OF THE SUBMITTED FORM: [form-id:value] if using Tokens (Replacement Patterns), or form-id if using Data Selector
- Value for WEBFORMS: choose the webform you wish to use
- Add an Action "Save PDF as file on server" (this action comes from the Views PDF module):
- Value for VIEW: choose the View you created above
- Value for VIEWS ARGUMENTS: [data:sid]
this is each webform's unique submission ID, and is used as the contextual filter on the view
- Value for STORE PATH: sites/default/files/viewspdf/info-[data:sid]
- change this to whatever you like; adding the token [data:sid] is optional, but ensures that each filename is unique.
- Important: do NOT add ".pdf" to the end, or you will get an error with "TCPDF ERROR: Unable to create output file ..."
- also do not add a forward slash at the beginning or TCPDF will throw the error above
- make sure the directory you specify exists and has write permissions
- Add an Action "Send HTML e-mail" (this action comes from the Mimemail Actions module):
- Value for KEY: [form-id:value] if using Tokens (Replacement Patterns), or form-id if using Data Selector
- Value for TO: the recipient's email address
- Value for ATTACHMENTS: sites/default/files/viewspdf/info-[data:sid].pdf
- Important: this matches what you entered above in STORE PATH, but includes .pdf at the end
- Value for all other fields: whatever you need