LCS - Working with PDFs

This guide will walk though how to generate a PDF file from a model data and setup a button for a user to download it.

It is often useful to be able to use model data to generate a document for a user and the format most users request is PDF. This guide expects you to know how to use models, create forms, basic actions, and interactions. If these are not concepts you have used before in Betty Blocks, see these KBs first and then return to this guide. 

Step 1: Create the model property

  1. The first task will be to update our data model to allow us to accept the new file upload where we will save the PDF. 
    1. Select the Model that will contain the link to the uploaded file and add a property with the type “file”. Examples if the property name could include “File upload”, “PDF file”.  
    2. If the file should not be publicly available, make sure to not check the box for public.

      screenshot of model property

Step 2: Add generate button to the page

  1. With the Model Property created we can go to the page on which the file will be generated and downloaded from. (On the Left side of Betty Blocks, click on Pages and either select the correct page or make a new page)  
  2. In the left side pane in the component view, search for and drag an action button on to the page.  
    1. This action button should exist inside of a Data Container, Data List Item or Data table column to gain access to the Model. 
    2. In the button configuration select the ID of the model that will be used to generate the PDF from.

      screenshot of action button


Step 3: Creating the action

  1. Open the action associated with the action button and follow standard naming rules and set the permissions that your application requires.
    1. PDF Generation will require an Action Step from the Block Store to be installed into the application.  
      1. Click on the Block store, in the search box enter “pdf”.  
      2. Now click on the “Generate PDF” block and on the next page click install block. As with all block installs select the correct Organization and application.

        screenshot of blockstore generate pdf

    2. Once installed close the Blocks store tab and in the action refresh the page to see the Generate PDF step as an option. 
    3. In start add our Input variable if we selected an Id of a model in Action button our input variable will also be “id”. Then in Action variables create and new record variable that uses a filter rule to match the model.id equal to the input variable “id”.

      screenshot of action variable


    4. With our data initialized drag a Generate PDF step to the action. And click on the step to view the following sidebar. 

      screenshot of step sidebar

      1. Populate the Variables section with relevant fields from the model that are needed to display in the pdf. 
      2. Select the model that will hold the PDF file and select the file field that will hold the PDF information. 
      3. To generate the body of the PDF there are 3 choices. HTML, HTML & CSS and LiquidJS templates. We will cover LiquidJS in a separate document. For this basic example we will just use HTML.
      4. Using this very generic HTML file you can see how we can add Fields from our models to the PDF document that will then get generated. 

        <html>
        <body>
        <h1>{{name}}</h1>
        <h6>created at: {{created_at}}</h6>
        <p>{{desc}}</p>
        </body>
        </html>
         

      5. Lastly Select a filename, this can be as simple as a basic file name, or you can build a file pattern with model data. Remember, to use model data to dynamically generate a name, an expression will need to concatenate the values together. 

Step 4: Displaying the download button

  1. With the action now complete it is time to return to the page and create the button to open the PDF file. 
    1. The first step is to click on the Generate PDF button and add an “onActionSuccess” interaction to re-fetch the data container, data List or data table containing our button. 
    2. With the Interaction complete, from the component list, search for and drag a conditional component on to the page next to the Generate PDF button. 
      1. Configure the conditional component so the Left is the file property from our model, Betty Blocks automatically sets it to be the URL of the file. Set the compare to be not equal and then leave the right empty. This will only show the content inside the conditional if the file URL has content. 
    3. Drag an Open page button inside the conditional. In the dialog box select save without configuration. 
      1. Click on the button and in the component details pane select “External Page”. 
      2. If the PDF should open in a new tab, select “New Tab”. 
      3. For the URL, select the Blue “+” sign and select the correct file property. Betty Blocks will automatically set this to be the PDF URL. 

        screenshot of open pdf button

After following these steps, you should now have a page that allows a user to generate a PDF for a given model and if that file exists a button to open/download it.  

Note: If the user clicks the generate PDF button additional times the PDF will be recreated. If that is unintended you will need to create conditional logic to disable or hide the Generate button once a PDF exists. 



Keywordscreate report, generate pdf, create pdf   Doc ID135217
OwnerJoel H.GroupLow Code Solutions
Created2024-02-05 09:59:24Updated2024-02-06 14:25:49
SitesDoIT Enterprise Business Systems - Low Code Solutions
Feedback  0   0