export-dashboard-pdf.ts•5.46 kB
/**
* MCP Tool: tableau_export_dashboard_pdf
*
* Purpose: Export a Tableau dashboard/view as PDF with optional filters
* Parameters:
* - viewId (required): View/dashboard identifier
* - filters (optional): Object with filter values to apply
* - pageType (optional): Paper size (a4, letter, legal, etc.)
* - orientation (optional): Portrait or landscape
* Returns: PDF file as base64-encoded string with metadata
*/
import { z } from 'zod';
import { TableauClient } from '../tableau-client.js';
import { TableauExportOptions } from '../types.js';
// Zod schema for input validation
export const ExportDashboardPDFArgsSchema = z.object({
viewId: z.string().describe('View/dashboard identifier to export'),
filters: z.record(z.string()).optional().describe('Optional filter values to apply (e.g., { "Region": "West", "Year": "2024" })'),
pageType: z.enum(['a4', 'letter', 'legal', 'tabloid', 'ledger', 'executive', 'folio']).optional().describe('Paper size for PDF export'),
orientation: z.enum(['portrait', 'landscape']).optional().describe('Page orientation for PDF export'),
});
export type ExportDashboardPDFArgs = z.infer<typeof ExportDashboardPDFArgsSchema>;
/**
* Handler for tableau_export_dashboard_pdf tool
*
* Exports a Tableau dashboard or view as a PDF file with optional filter values
* and page configuration. Returns the PDF as base64-encoded string.
*
* @param args - Tool arguments (viewId, filters, pageType, orientation)
* @param tableauClient - Initialized Tableau client instance
* @returns Formatted response with PDF data or error
*/
export async function exportDashboardPDFHandler(
args: ExportDashboardPDFArgs,
tableauClient: TableauClient
): Promise<{ content: Array<{ type: string; text: string }> }> {
try {
// Prepare export options
const options: TableauExportOptions = {};
if (args.pageType) {
options.pageType = args.pageType;
}
if (args.orientation) {
options.orientation = args.orientation;
}
// Call TableauClient to export dashboard as PDF
const pdfBuffer = await tableauClient.exportDashboardPDF(
args.viewId,
args.filters,
options
);
// Convert Buffer to base64
const pdfBase64 = pdfBuffer.toString('base64');
const pdfSizeKB = (pdfBuffer.length / 1024).toFixed(2);
// Create a summary
const filterInfo = args.filters && Object.keys(args.filters).length > 0
? `\nFilters applied:\n${Object.entries(args.filters)
.map(([key, value]) => ` - ${key}: ${value}`)
.join('\n')}`
: '\nNo filters applied';
const summary = `PDF Export Successful\n\n` +
`View/Dashboard ID: ${args.viewId}\n` +
`Page Type: ${args.pageType || 'default'}\n` +
`Orientation: ${args.orientation || 'default'}\n` +
`File Size: ${pdfSizeKB} KB\n` +
filterInfo +
`\n\nThe PDF has been exported and encoded as base64. ` +
`You can decode and save it using the provided base64 data.`;
// Return summary and base64 data
// Note: For very large PDFs, you might want to stream or provide a download URL instead
return {
content: [
{
type: 'text',
text: summary
},
{
type: 'text',
text: `\n\nBase64 PDF Data (first 500 characters):\n${pdfBase64.substring(0, 500)}...\n\n[PDF data truncated for display. Full size: ${pdfSizeKB} KB]`
},
{
type: 'text',
text: `\n\nFull Base64 Data:\n${pdfBase64}`
}
]
};
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
return {
content: [{
type: 'text',
text: `Error exporting dashboard to PDF: ${errorMessage}\n\n` +
`Troubleshooting:\n` +
`- Verify the view ID is correct\n` +
`- Check that the view is accessible\n` +
`- Ensure filter names match dashboard filter names exactly\n` +
`- Confirm PDF export is enabled for this view\n` +
`- Large dashboards may take longer or timeout`
}]
};
}
}
// Tool metadata for MCP server registration
export const exportDashboardPDFTool = {
name: 'tableau_export_dashboard_pdf',
description: 'Export a Tableau dashboard or view as PDF with optional filters and page configuration. Supports various paper sizes (a4, letter, legal, etc.) and orientations (portrait, landscape). Filters can be applied by providing a filter object with key-value pairs. Returns PDF as base64-encoded string.',
inputSchema: {
type: 'object',
properties: {
viewId: {
type: 'string',
description: 'View/dashboard identifier (GUID) to export as PDF'
},
filters: {
type: 'object',
description: 'Optional: Filter values to apply before export (e.g., {"Region": "West", "Year": "2024"}). Filter names must match dashboard filter names exactly.',
additionalProperties: {
type: 'string'
}
},
pageType: {
type: 'string',
enum: ['a4', 'letter', 'legal', 'tabloid', 'ledger', 'executive', 'folio'],
description: 'Optional: Paper size for PDF export (default: a4)'
},
orientation: {
type: 'string',
enum: ['portrait', 'landscape'],
description: 'Optional: Page orientation for PDF export (default: portrait)'
}
},
required: ['viewId']
}
};