Skip to main content
In this tutorial, you’ll learn how to create an app that converts Domo dashboards into downloadable PDFs using Domo’s Code Engine and Pro-Code Editor. This app allows users to specify a Domo Page ID, apply filters, and generate a consolidated PDF containing the dashboard tables and charts. This guide assumes familiarity with JavaScript, Pro-Code Editor, and Code Engine Functions.

Prerequisites

  • Access to Domo’s Pro-Code Editor.
    • To get access to the beta, please reach out to your CSM.
  • Basic understanding of JavaScript and the HTML2PDF.js library.
  • Code engine enabled in your Domo instance.

Step 1: Set Up Your Pro-Code Environment

  1. Create a New Project in Pro-Code Editor:
Ensure that Pro-Code Editor is enabled in your Domo instance. Navigate to your Asset Library. asset_library Click on ‘Pro-Code Editor’ in the top right corner of your screen to open the editor in your browser. pro_code_button You can now edit the files in your project. pro_code_editor

Step 2: Define the Manifest File (manifest.json)

The manifest.json file defines your app’s metadata and data mappings in Domo. Update it according to your dataset’s structure.
We are not using any datasets in the app, but we do need to wire it to be able to access the page filters in a domo dashboard. Code engine functions are used to call the api to generate the encoded pdf that we will decode and build the pdf in the app. Make sure to use the version of your code engine that is functional. code-engine-manifest

Step 3: Handling PDF Generation within Code Engine

3.1. Setup Code Engine

  • Navigate to WorkFlows in your environment and, on the left side, click on the Code Engine icon.
workflows-navigate code-engine-navigate
  • Create a new Code Engine package, use Javascript as the language.
create-code-engine name-language-code
  • Everytime you want to use the function, you have to save it and deploy it. Click on the arrow next to the save button on the upper right corner, and click on Deploy.
deploy

3.2. Setup Code Engine Helpers

  • Import Code Engine:
    • Start by importing the codeengine module, which will handle all the API requests within your Code Engine function.
    • Define a Helpers class that includes a method for making HTTP requests (handleRequest). This method will streamline how API calls are made and manage any errors that might occur during the process.

3.3. Retrieve Cards from a Page

  • Function: getCardsOnPage :
    • This function retrieves all the cards on a given page by calling the /api/content/v1/pages/{pageID}/cards endpoint.
    • It utilizes the Helpers.handleRequest method to make the API call and returns the list of cards.
  • Set up the inputs and outputs of the function.

Input Parameters

  • pageID: string

OUTPUT Parameters

  • result: array
get-cards

3.4. Generate PDF for Each Card

  • Function: getPDF :
    • This function is responsible for generating a PDF for a specific card using the card’s ID.
    • It makes a PUT request to the /api/content/v1/cards/kpi/{cardID}/render endpoint, passing any necessary parameters, such as filters, to customize the PDF output.

Input Parameters

  • cardID: string
  • params: object

OUTPUT Parameters

  • result: array
get-pdf

3.5. Convert Page to PDF

  • Function: page2PDF :
    • This is the core function that brings everything together. It:
      1. Calls getCardsOnPage to retrieve all the cards on the page.
      2. Iterates through each card and calls getPDF to generate a PDF for each one.
      3. Handles different chart types, setting custom dimensions for the generated PDFs.
      4. Collects all the generated PDFs into a pdfResult array.
      5. Returns the final dashboard result, which includes the pdfResult and any relevant metadata.

Input Parameters

  • pageID: string
  • filters: object

OUTPUT Parameters

  • dashboardResult: object
page-pdf

Step 4: Configure index.html

The index.html file serves as the main interface for your app. It includes an input field for Page ID, a button to trigger PDF conversion, and a result message.

Step 5: Styling with app.css

The app.css file defines styles for the app’s layout and elements.

Step 6: Write JavaScript Logic in app.js

The app.js file handles interactions like managing filters, merging PDFs, and triggering the PDF download.

6.1: Initialize Variables and Event Listeners

6.2: Handle Filters

Manage and display the filters applied to the data.

6.3: Start PDF Conversion

This function triggers the PDF generation and handles user feedback on the result. It uses the Domo Code Engine function to generate the encoded PDF and call the merge function to build the pdf document with all tables and charts in the dashboard.

6.4: Merge PDFs

This function merges individual PDF pages into a single document and downloads it. PDF-Lib is used for merging multiple PDFs, reordering or resizing pages, and adding customized content like titles or footers. Whereas HTML2PDF is used to generate downloadable PDF files from HTML content Any time you want to reference the documentaions, please access the HTML2PDF documentation and PDF-Lib documentation.
  1. Initialize PDF-lib and Create a New Document:
    • Start by importing the PDF-lib library.
    • Create a new PDFDocument object called mergedPdf to hold all pages from the PDFs being merged.
  2. Clean Up the Title String:
    • Clean up the titleHtml string by removing any HTML tags and replacing special characters with newlines to ensure a clear, readable title in the final PDF.
  3. Iterate Over Each PDF Result:
    • Create an array named pdfArray, with each element (pdfResult from the code engine function response) as a base64-encoded PDF string.
    • Loop through each pdfResult in the array, decode the base64 string, and load the PDF data into a new PDFDocument.
    • Retrieve all pages from the loaded PDF document.
  4. Conditional Title Addition:
    • Add a title to the PDF if the titleHtml string is not empty.
  5. Copy Pages to Merged PDF:
    • For each page in the loaded PDF, copy it to the mergedPdf document, accumulating all pages into a single document.
  6. Create a New Consolidated PDF Document:
    • After merging pages into mergedPdf, create another PDFDocument object named consolidatedPdf.
    • Add a title page to consolidatedPdf that displays the cleaned-up title and any applied filters.
  7. Organize Pages Based on Size:
    • Loop over each page in mergedPdf and add it to consolidatedPdf, positioning based on the page’s width.
  8. Save and Download the Consolidated PDF:
    • Save consolidatedPdf as a byte array and trigger a download of the final merged PDF using a download function.

Conclusion

By following this tutorial, you’ve built a functional app that converts Domo dashboards into downloadable PDFs, handling filters and merging multiple PDFs into a single file. This app can be further enhanced by adding more customization options and error handling. final-app