Button using the Function that creates a PDF from Workshop

I’m working in Palantir Foundry’s Workshop and have a function that generates a PDF from a dashboard. I’d like to trigger this function using a button, but I’m struggling to wire it up correctly.

Specifically, I’ve added a button widget, and under the “ON CLICK” configuration, I’m presented with several options: ACTION, EVENT, EXPORT, SCENARIO EVENT, FUNCTION BACKED EXPORT, and EXPORT MEDIA. I’ve tried all of them, but none successfully trigger my function.

My question: How can I configure a Workshop button to trigger a custom function that generates a PDF?

If possible, I’d appreciate guidance on:

  • Which “ON CLICK” type is correct for triggering custom TypeScript logic

Here’s the code I’m trying to trigger:

import { Function, StringPropertyBaseType } from "@foundry/functions-api";
import { PDFDocument, rgb, StandardFonts } from "pdf-lib";

export class DashboardExportFunctions {
    @Function()
    public async exportPDF(dashboardRID: string): Promise<boolean> {
        try {
            const pdfBase64 = await this.exportDashboardToPDF(dashboardRID);
            const pdfBlob = this.base64ToBlob(pdfBase64, 'application/pdf');
            const url = URL.createObjectURL(pdfBlob);
            const a = document.createElement('a');
            a.href = url;
            a.download = 'dashboard.pdf';
            a.click();
            URL.revokeObjectURL(url);
            return true; // Indicate success
        } catch (error) {
            console.error('Error exporting PDF:', error);
            return false; // Indicate failure
        }
    }

    private async exportDashboardToPDF(dashboardRID: string): Promise<string> {
        const pdfDoc = await PDFDocument.create();
        const page = pdfDoc.addPage();
        const { width, height } = page.getSize();
        const font = await pdfDoc.embedFont(StandardFonts.Helvetica);

        page.drawText('Dashboard Export', {
            x: 50,
            y: height - 50,
            size: 30,
            font: font,
            color: rgb(0, 0, 0),
        });

        const pdfBytes = await pdfDoc.save();
        return Buffer.from(pdfBytes).toString('base64');
    }

    private  base64ToBlob(base64: string, mimeType: string): Blob {
        const byteCharacters = atob(base64);
        const byteNumbers = new Array(byteCharacters.length);
        for (let i = 0; i < byteCharacters.length; i++) {
            byteNumbers[i] = byteCharacters.charCodeAt(i);
        }
        const byteArray = new Uint8Array(byteNumbers);
        return new Blob([byteArray], { type: mimeType });
    }
}

Any direction or example would be greatly appreciated — this seemingly simple task has cost me hours.

There is no general on click type for triggering a custom function. You can use functions in specific ways in Workshop, such as to back table columns, or to back a variable value, but Workshop needs to know what the result of the function should do.

This case sounds like a function backed export, but we currently do not offer an export to PDF option.

We have an issue tracking the request for more supported file types including PDFs internally, which I’ll add this signal to.

One workaround you could explore is to use a function backed action to save the PDF on an object as an attachment property. A second “Export media” button click could then be configured to download that PDF.

2 Likes

Thank I will give it a try

Any luck doing this @Palmaker?
I am interested in doing the same.

1 Like

One possible alternative could be to embedd a custom slate app that handles the pdf export.

I remember that we did such workaround in the past on a case were we could not invoke some specific functions directly from workshop.
The slate app was embedded in a hidden section with infinitely small size. The button click just set the section visible which automatically invokes the function(s) and returned the resulting variables through interface variables. The section visibility variable was an interface variable itself and the slate app set it to false again (basically we just toggled the visibility for the short duration of function execution)

I am not sure if such pdf export functions exists in slate as per your needs but maybe this proposal opens up new options for you.

Bobby, I use a NPM PDF-Me library and that works to save a dashboard as PDF

2 Likes

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.