Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

2.2 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { HttpMethod } from '@activepieces/pieces-common';
import { documergeAuth } from '../common/auth';
import { DocuMergeClient } from '../common/client';

export const convertFileToPdf = createAction({
  auth: documergeAuth,
  name: 'convert_file_to_pdf',
  classification: 'READ',
  displayName: 'Convert File to PDF',
  description: 'Convert a given file to PDF',
  audience: 'both',
  aiMetadata: { description: 'Convert a single source file to PDF on DocuMerge, supplying the file by name plus an optional remote URL or inline contents. Use when an agent needs a PDF rendering of another document format. Requires a file name; each call generates a fresh PDF (not idempotent).', idempotent: false },
  props: {
    fileName: Property.ShortText({
      displayName: 'File Name',
      description: 'Name of the file to convert',
      required: true,
    }),
    fileUrl: Property.ShortText({
      displayName: 'File URL',
      description: 'URL of the file to convert (must be a valid URL)',
      required: false,
    }),
    contents: Property.LongText({
      displayName: 'Contents',
      description: 'Additional content to include',
      required: false,
    }),
  },
  async run(context) {
    const { fileName, fileUrl, contents } = context.propsValue;

    if (!fileName) {
      throw new Error('File name is required');
    }

    const client = new DocuMergeClient(context.auth.secret_text);

    const body: Record<string, unknown> = {
      file: {
        name: fileName,
      },
    };

    if (fileUrl) {
      (body['file'] as Record<string, unknown>)['url'] = fileUrl;
    }

    if (contents) {
      body['contents'] = contents;
    }

    const fileData = await client.makeBinaryRequest(
      HttpMethod.POST,
      '/api/tools/pdf/convert',
      body
    );

    const pdfFileName = fileName.endsWith('.pdf')
      ? fileName
      : `${fileName.replace(/\.[^/.]+$/, '')}.pdf`;

    const fileUrlResult = await context.files.write({
      fileName: pdfFileName,
      data: Buffer.from(fileData),
    });

    return {
      success: true,
      fileName: pdfFileName,
      fileUrl: fileUrlResult,
      format: 'pdf',
      size: fileData.byteLength,
    };
  },
});