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:

typescript

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

export class DashboardExportFunctions {
@Function()
public async exportPDF(dashboardRID: string): Promise {
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