|
| 1 | +import { task } from "@trigger.dev/sdk"; |
| 2 | +import { execFile } from "node:child_process"; |
| 3 | +import { mkdirSync, readFileSync, unlinkSync, writeFileSync } from "node:fs"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import { promisify } from "node:util"; |
| 7 | + |
| 8 | +const execFileAsync = promisify(execFile); |
| 9 | + |
| 10 | +/** |
| 11 | + * Convert a .docx or .pptx file (supplied as a URL) to PDF using LibreOffice |
| 12 | + * running in headless mode — no X11 display required. |
| 13 | + * |
| 14 | + * Requires the `libreoffice()` build extension in trigger.config.ts so that |
| 15 | + * LibreOffice is available inside the deployed container. |
| 16 | + */ |
| 17 | +export const libreofficeConvert = task({ |
| 18 | + id: "libreoffice-convert", |
| 19 | + run: async (payload: { |
| 20 | + /** Public URL of the .docx or .pptx file to convert. */ |
| 21 | + documentUrl: string; |
| 22 | + /** Optional output filename (without extension). Defaults to "output". */ |
| 23 | + outputName?: string; |
| 24 | + }) => { |
| 25 | + const { documentUrl, outputName = "output" } = payload; |
| 26 | + |
| 27 | + // Use a unique temp directory so concurrent runs don't collide. |
| 28 | + const workDir = join(tmpdir(), `lo-${Date.now()}`); |
| 29 | + mkdirSync(workDir, { recursive: true }); |
| 30 | + |
| 31 | + // Derive a safe input filename from the URL. |
| 32 | + const urlPath = new URL(documentUrl).pathname; |
| 33 | + const ext = urlPath.split(".").pop() ?? "docx"; |
| 34 | + const inputPath = join(workDir, `input.${ext}`); |
| 35 | + // LibreOffice names the output after the input file stem. |
| 36 | + const outputPath = join(workDir, `input.pdf`); |
| 37 | + |
| 38 | + try { |
| 39 | + // 1. Download the source document. |
| 40 | + const response = await fetch(documentUrl); |
| 41 | + if (!response.ok) { |
| 42 | + throw new Error(`Failed to fetch document: ${response.status} ${response.statusText}`); |
| 43 | + } |
| 44 | + const arrayBuffer = await response.arrayBuffer(); |
| 45 | + writeFileSync(inputPath, Buffer.from(arrayBuffer)); |
| 46 | + |
| 47 | + // 2. Convert to PDF using LibreOffice headless. |
| 48 | + // --norestore prevents LibreOffice from showing a recovery dialog. |
| 49 | + // --outdir directs the output file to our working directory. |
| 50 | + const libreofficeBin = process.env.LIBREOFFICE_PATH ?? "libreoffice"; |
| 51 | + await execFileAsync(libreofficeBin, [ |
| 52 | + "--headless", |
| 53 | + "--norestore", |
| 54 | + "--convert-to", |
| 55 | + "pdf", |
| 56 | + "--outdir", |
| 57 | + workDir, |
| 58 | + inputPath, |
| 59 | + ]); |
| 60 | + |
| 61 | + // 3. Read the resulting PDF. |
| 62 | + const pdfBuffer = readFileSync(outputPath); |
| 63 | + |
| 64 | + return { |
| 65 | + outputName: `${outputName}.pdf`, |
| 66 | + sizeBytes: pdfBuffer.byteLength, |
| 67 | + // Return base64 so the result is JSON-serialisable. |
| 68 | + // In production you would upload pdfBuffer to S3 / R2 instead. |
| 69 | + base64: pdfBuffer.toString("base64"), |
| 70 | + }; |
| 71 | + } finally { |
| 72 | + // Clean up temp files. |
| 73 | + try { |
| 74 | + unlinkSync(inputPath); |
| 75 | + } catch {} |
| 76 | + try { |
| 77 | + unlinkSync(outputPath); |
| 78 | + } catch {} |
| 79 | + } |
| 80 | + }, |
| 81 | +}); |
0 commit comments