How To: Use the Save As PDF Control and Show a Message to Confirm the File
This article illustrates how to use the Save As PDF control on a view, showing a message after the control method executes and the PDF file is created. The Save As PDF control creates a snapshot of the browser window including any values entered into form fields. Use this control, for example, to provide a copy of a form or a receipt as a PDF.
Show a message to confirm the PDF file
Scenario
You work in Human Resources. You created an online form for employees to submit leave requests. For employee convenience, your form includes an option to save a copy of the request in PDF format.
Steps
For this scenario, you create a SmartBox SmartObject to store request details. You create a view from the SmartObject, then add the Save As PDF control. You add a button and configure a rule that executes the PDF control method when clicked. Finally, you add an action that shows a confirmation message after the PDF method executes.
Setup Steps
Use the following steps to create the scenario from start to finish. If you want to skip the setup steps and view the topic step, go to Add the Save As PDF Control and Configure a Rule for Executing the Control Method.
- From K2 Designer, create a SmartBox SmartObject called Leave Requests. Add at least three properties. (Optional: Create categories to keep your work organized.)
- Launch K2 Designer.If you do not know how to access K2 Designer, see Accessing K2 Sites.
- From the All Items node, create categories to store your work. Categories are not required, but help to keep your work organized.
- Create a SmartBox SmartObject called Leave Requests. Add at least three properties. (Use the image below as a guide, if necessary.) Finish the SmartObject to save and exit.
- Launch K2 Designer.
- Generate an item view from the Leave Requests SmartObject and name it Leave Requests Item.
- Right-click the Leave Requests SmartObject and select Generate Views.
- Check the option for Item view and keep the default name, Leave Requests Item. Click OK.
Add the Save As PDF Control and Configure a Rule for Executing the Control Method
The following step describes how to add the control and configure a rule to integrate with the control.
- Edit the Leave Requests Item view and add the Save As PDF control to the top of the design canvas. Add a Button control (to the right of the PDF control) and change the Name to Create PDF Button. Change the Text to Create PDF. Add and configure a rule using the table below as a guide.
Rule/Action Configuration Notes New Rule: When a control on the View raises an event Control = Create PDF Button;
Event = Clicked
This rule executes when you click the Create PDF button. New Action: Execute a control's method Control = Save As PDF;
Method = Save PDF
This control has two methods: Save PDF executes synchronously while Save PDF Async executes asynchronously. For more information and considerations, see Save As PDF Control in the User Guide. New Action: Show a message Configure the message so that it confirms the successful creation of the PDF file. This action serves two purposes. It informs the user that their PDF file is ready for use. It also confirms that there were no errors during the file creation as this action executes after the PDF is created. New Action: Execute a method on the View Method = Clear This clears the view fields and allows you to enter another request. - Edit the Leave Requests Item view. On the left side of your screen, click the Toolbox tab. Locate the Export heading, then drag the Save As PDF control onto the design canvas at the top of the view.
- In the Toolbox, locate the Action heading. Drag the Button control onto the canvas to the right of the PDF control.
- With the button control selected, change the Name of the control to
Create PDF Button
and change the control Text to
Create PDF. - Click the Rules tab. Click Add Rule. With the Events tab active, locate the Control Events heading and click When a control on the View raises an event to add it to the rule definition pane. Click the select control link and change it to Create PDF Button. The select event link updates to Clicked. This rule executes when you click the Create PDF Button control.Rules consist of events, conditions, and actions. A valid rule contains at least one action.
- Events define when the rule should run, such as when a button is clicked, or when the form loads. (Events are optional, you can define "event-less" rules that you can call from other rules.)
- Conditions define whether the rule should run. If the criteria are true, continue the rule, and if the criteria are not met, stop the rule. For example, a condition might evaluate a form to confirm that required fields have content. (Conditions are optional - not all rules will require conditions.)
- Actions define what the rule should do. For example, show a message, start a workflow, or enable a form field. Rules can contain multiple actions that are run in sequence or in parallel.
- This rule does not contain any conditions, so click the Actions tab. You want to execute the method that creates a PDF file. Locate the Control Interaction heading and click Execute a control's method to add it to the rule definition pane. Click the select control link and change it to Save As PDF. Click the select method link and change it to Save PDF.The Save As PDF control has two methods: Save PDF and Save PDF Async.
Save PDF: This option runs synchronously or in a linear fashion during which the view or form controls become locked while the action runs.
Save PDF Async. This option runs asynchronously, otherwise known as fire-and-forget. You can edit view controls while the action runs.
For more information and considerations, see Save As PDF Control in the User Guide.
- Still on the Actions tab, locate the Notifications heading and click Show a message to add it to the rule definition pane. (You can also search for the action by entering a keyword into the search box.)
- Click (configure). Use the image below as a guide to configure your message to let the user know their PDF file is ready. Click OK.
- Finally, you add an action that clears the view controls. This leaves you with an empty view that you can use to submit another request. If you entered a search keyword, delete it now. The list of actions are reset to the default order. Click Execute a method on the View to add it to the rule definition pane. Select the Clear method. Click OK to save the rule. Click Finish to save and exit the view.
With the controls in place, you turn your attention to rules. You add a rule that executes when you click the Create PDF button. This rule has three actions: the first executes the method that creates the PDF file, the second shows a message that confirms the file creation, and the third clears the view so that you can start another submittal.
Next, you add an action to show a message. This is where the synchronous and asynchronous methods come into play. You want the message to confirm the PDF file creation is successful, so you want to see the message after the method completes, so you choose synchronous which locks other actions from running until the method completes.
- Edit the Leave Requests Item view. On the left side of your screen, click the Toolbox tab. Locate the Export heading, then drag the Save As PDF control onto the design canvas at the top of the view.
- Test the view by using the Runtime URL. Enter content into the view controls (except the ID), then click the Create PDF button. You see the confirmation pop-up after the PDF file is created. Still in K2 Designer, navigate to the System > Controls > Save As PDF > PDF File SmartObject. Generate a list view from the SmartObject. Run the list view and observe the saved PDF file. This is the system SmartObject K2 uses when creating PDF files using the Save As PDF control.
- To test the view, click the Runtime URL. The view opens in your default web browser, where you can test controls and rules. Ultimately, you must contain views in forms for use by other users.
- Complete the view fields, with the exception of ID. (The ID is an autonumber that K2 adds when creating a record in the SmartObject.) Click Create PDF. You see the confirmation message (that appears after K2 creates the PDF file). Click OK. Notice the view fields clear, allowing you to input new details.
- Still in K2 Designer, navigate to the System > Controls > Save as PDF category.If you do not see system categories, you may need to enable them. At the bottom of the column, locate the Show: (All) link. Click (All) and check the box for System Objects.
- Notice the PDF File SmartObject. This is the system SmartObject K2 uses to store PDF files created from the Save As PDF control. Generate a list view from the SmartObject and name it
PDF File List
then click OK. - Select the PDF File List view, then click Run (in the central pane). The PDF file names reflect the date and time they were created. (You may see more than one record, depending on the use of the PDF control.) Click the link to the file created earlier, then look at the details captured by the PDF.
Last, you check the PDF file created. K2 stores the file in a system SmartObject called PDF File. You can create a list view from this SmartObject, then run it to view all PDF files created with the PDF control.
- To test the view, click the Runtime URL. The view opens in your default web browser, where you can test controls and rules. Ultimately, you must contain views in forms for use by other users.
Review
The Save As PDF control takes a snapshot of your browser and saves it to a system SmartObject. The control has two methods: one executes synchronously and the other asynchronously. The synchronous method locks the browser and you cannot make any changes to form fields while the method runs. The asynchronous method is a fire-and-forget format that runs in the background, allowing you to continue editing form fields. Your environment and security policies should guide you in choosing the appropriate method.