Audience
  • Developers
  • Form Designers
Required Knowledge
  • Views
  • Rules
  • Controls

Introduction

The latest How To topics are available in the K2 Five User Guide or the K2 Cloud User Guide’s How to section.

The Save as PDF control is now available and is used to take a snapshot of the View/Form at a particular point in time when an Event is executed, for example when a Button is clicked. The PDF snapshot is saved to a SmartObject and executed by using one of the Execute Control Method actions. For more information on using the Save as PDF control, see the following topic in the help guide:

K2 smartforms > K2 Designer > Views > Creating Views > Layout > Toolbox > Controls > Save as PDF Control

This KB article explains the differences between the methods available for use with the Save as PDF control and the affect the different execution types have when used with these methods.  

The Save as PDF control contains two methods:

  • SavePDF - This method executes synchronously. Used when user-defined actions should happen before or after execution of an event. While executing the method, the browser is locked but the Key Property's value is immediately available. This method is useful when the Key Property's value is required in the same Rule.
  • SavePDFAsync - This method executes asynchronously. Used when user-defined actions should happen during execution of an event. While executing the method, the Key Property's value is not available. After execution of the method, the When [Save as PDF] is Completed event is raised and the Key Property's value becomes available. This method is useful when you do not need the Key Property's value or you do not want to lock up the browser.

The following Events are available for both methods mentioned above:

  • When [Save as PDF] is Started - Raised when the PDF is being created. If you are changing values or the HTML of the page you should wait until this event is raised to prevent the changes from coming through to the PDF. For most users this will not be an issue and the event can be ignored.
  • When [Save as PDF] is Completed - Raised when the PDF has been created. This means the control is done executing one of its methods and its value is set to the new Key Property’s value. For users that want to get the Key Property’s value and are using the SavePDFAsync method, they must wait for this event before it will be available.

Implementation Details

The Events are available in the Rule Designer after using the SavePDF or SavePDFAsync methods. An Action can be configured to execute before or after the PDF is created.

One example is where the SavePDFAsync method is used and you want to add a Label to the document before it is saved as a PDF to indicate that it's a copy of the original document. A Rule can be configured to show the Copy Label control before the PDF is saved.

The Rules will look like this:

Event: When a control on the View raises and Event

Action: Execute a control's method

Image

Event: When a control on the View raises and Event

Action: Show a control

Image

Example Scenarios

Scenario 1

We will use a View with the Save as PDF control to show a message upon start of the PDF creation, as well as at the end of the PDF creation.

Asynchronous Execution - When user-defined actions should happen during execution of an event.

Follow the steps below to set up the scenario as described above:

  1. Create a SmartObject. We used a Leave SmartObject.
  2. Create an Item View from the SmartObject and add a Save as PDF control.
  3. Add a Button control and rename the control to Create PDF.
    Image
  4. Configure the Button control’s Rule as follows:

    Events: When a control on a View raises an event

    Actions: Execute a control’s method

    Image
  5. Add a Rule for the Started event as shown below:

    Events: When a control on a View raises an event

    Actions: Show a message

    Click on configure next to the message action to specify the details of the message. This message will be sent to inform the user that the PDF creation has started.
    Image
  6. Add a Rule for the Complete event as shown below:

    Events: When a control on a View raises an event

    Actions: Show a message

    Click on configure next to the message action to specify the details of the message. This message will be sent to inform the user that the PDF creation has been completed.
    Image
  7. Finish the View then Run the View
  8. Capture some information and click on the Create PDF Button.
  9. The first message appears. Click OK.
    Image
  10. The second message appears. Click OK.
    Image
  11. Create a List View from the PDF File SmartObject to view the PDF created. The PDF File SmartObject can be accessed in the Category tree in the following location:
    Image

NOTE: The popups are shown in correct succession. If the popup is not closed by the time the PDF is captured the PDF will display the modal and popup.

Synchronous Execution – Using the same scenario as above with synchronous execution

  1. Create a new SmartObject or use the SmartObject created earlier.
  2. Create an Item View with a Save as PDF control.
  3. Add a Button control and rename the control to Create PDF.
    Image
  4. Configure the Button control’s Rule as follows:

    Events: When a control on a View raises an event

    Actions: Get confirmation from user

    Actions: Execute a control’s method

    Use an Execution type of one after another

    Image
  5. Configure the heading and message as shown below:
    Image
  6. Add a Rule for the Complete event as shown below:

    Events: When a control on a View raises an event

    Actions: Show a message

    Click on configure next to the message action to specify the details of the message. This message will be sent to inform the user that the PDF creation has been completed.
    Image
  7. Finish the View then Run the View.
  8. Capture some information and click on the Create PDF Button.
  9. The confirmation message appears. Click OK.
    Image
  10. The message appears to confirm that the PDF has been created. Click OK.
    Image

NOTE: This will show a popup before PDF creation and, depending on the user’s choice, the PDF’s Save method will execute. Using a confirmation before the execution of the method is advised for both scenarios as it ensures that the PDF creation process is free from any popups.

Scenario 2

We will use a View with the Save as PDF control and clear the View for new input after a PDF has been created.
Synchronous Execution - When user-defined actions should happen before or after execution of an event.

Follow the steps below to set up the scenario as described above:

  1. Create a new SmartObject or use the SmartObject created earlier.
  2. Create an Item View with a Save as PDF control.
  3. Add a Button control and rename the control to Create PDF.
    Image
  4. Configure the Button control’s Rule as follows:

    Events: When a control on a View raises an event

    Actions: Execute a control’s method

    Actions: Execute a method on the View

    Use an Execution type of one after another

    Image
  5. Finish the View then Run the View
  6. Capture some information and click on the Create PDF Button.
  7. The PDF is created and the screen is cleared to allow capturing of new information.

NOTE: With the synchronous execution there is no need to use any of the Save as PDF control’s events

 

Asynchronous Execution – Using the same scenario as above with asynchronous execution

  1. Create a new SmartObject or use the SmartObject created earlier.
  2. Create an Item View with a Save as PDF control.
  3. Add a Button control and rename the control to Create PDF.
    Image
  4. Configure the Button control’s Rule as follows:

    Events: When a control on a View raises an event

    Actions: Execute a control’s method

    Image
  5. Add a Rule for the Complete event as shown below:

    Events: When a control on a View raises an event

    Actions: Execute a method on the View

    Image
  6. Finish the View then Run the View
  7. Capture some information and click on the Create PDF Button.
  8. The PDF is created and the screen is cleared to allow capturing of new information.

NOTE: Having the Clear method directly after the Execute a control’s method action (as with the synchronous execution) will most probably result in the View being cleared before the PDF is created.