|
| 1 | +--- |
| 2 | +title: "Convert documents to PDF using LibreOffice" |
| 3 | +sidebarTitle: "LibreOffice PDF conversion" |
| 4 | +description: "This example demonstrates how to convert documents to PDF using LibreOffice with Trigger.dev." |
| 5 | +--- |
| 6 | + |
| 7 | +import LocalDevelopment from "/snippets/local-development-extensions.mdx"; |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +- A project with [Trigger.dev initialized](/quick-start) |
| 12 | +- [LibreOffice](https://www.libreoffice.org/download/libreoffice-fresh/) installed on your machine |
| 13 | +- A [Cloudflare R2](https://developers.cloudflare.com) account and bucket |
| 14 | + |
| 15 | +### Using our `aptGet` build extension to add the LibreOffice package |
| 16 | + |
| 17 | +To deploy this task, you'll need to add LibreOffice to your project configuration, like this: |
| 18 | + |
| 19 | +```ts trigger.config.ts |
| 20 | +import { aptGet } from "@trigger.dev/build/extensions/core"; |
| 21 | +import { defineConfig } from "@trigger.dev/sdk/v3"; |
| 22 | + |
| 23 | +export default defineConfig({ |
| 24 | + project: "<project ref>", |
| 25 | + // Your other config settings... |
| 26 | + build: { |
| 27 | + extensions: [ |
| 28 | + aptGet({ |
| 29 | + packages: ["libreoffice"], |
| 30 | + }), |
| 31 | + ], |
| 32 | + }, |
| 33 | +}); |
| 34 | +``` |
| 35 | + |
| 36 | +<Note> |
| 37 | + [Build extensions](/config/config-file#extensions) allow you to hook into the build system and |
| 38 | + customize the build process or the resulting bundle and container image (in the case of |
| 39 | + deploying). You can use pre-built extensions or create your own. |
| 40 | +</Note> |
| 41 | + |
| 42 | +You'll also need to add `@trigger.dev/build` to your `package.json` file under `devDependencies` if you don't already have it there. |
| 43 | + |
| 44 | +## Convert a document to PDF using LibreOffice and upload to R2 |
| 45 | + |
| 46 | +This task demonstrates how to use LibreOffice to convert a document (.doc or .docx) to PDF and upload the PDF to an R2 storage bucket. |
| 47 | + |
| 48 | +### Key Features |
| 49 | + |
| 50 | +- Fetches a document from a given URL |
| 51 | +- Converts the document to PDF |
| 52 | +- Uploads the PDF to R2 storage |
| 53 | + |
| 54 | +### Task code |
| 55 | + |
| 56 | +```ts trigger/libreoffice-pdf-convert.ts |
| 57 | +import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3"; |
| 58 | +import { task } from "@trigger.dev/sdk/v3"; |
| 59 | +import libreoffice from "libreoffice-convert"; |
| 60 | +import { promisify } from "node:util"; |
| 61 | +import path from "path"; |
| 62 | +import fs from "fs"; |
| 63 | + |
| 64 | +const convert = promisify(libreoffice.convert); |
| 65 | + |
| 66 | +// Initialize S3 client |
| 67 | +const s3Client = new S3Client({ |
| 68 | + // How to authenticate to R2: https://developers.cloudflare.com/r2/api/s3/tokens/ |
| 69 | + region: "auto", |
| 70 | + endpoint: process.env.R2_ENDPOINT, |
| 71 | + credentials: { |
| 72 | + accessKeyId: process.env.R2_ACCESS_KEY_ID ?? "", |
| 73 | + secretAccessKey: process.env.R2_SECRET_ACCESS_KEY ?? "", |
| 74 | + }, |
| 75 | +}); |
| 76 | + |
| 77 | +export const libreOfficePdfConvert = task({ |
| 78 | + id: "libreoffice-pdf-convert", |
| 79 | + run: async (payload: { documentUrl: string }, { ctx }) => { |
| 80 | + // Set LibreOffice path for production environment |
| 81 | + if (ctx.environment.type !== "DEVELOPMENT") { |
| 82 | + process.env.LIBREOFFICE_PATH = "/usr/bin/libreoffice"; |
| 83 | + } |
| 84 | + |
| 85 | + try { |
| 86 | + // Create temporary file paths |
| 87 | + const inputPath = path.join(process.cwd(), `input_${Date.now()}.docx`); |
| 88 | + const outputPath = path.join(process.cwd(), `output_${Date.now()}.pdf`); |
| 89 | + |
| 90 | + // Download file from URL |
| 91 | + const response = await fetch(payload.documentUrl); |
| 92 | + const buffer = Buffer.from(await response.arrayBuffer()); |
| 93 | + fs.writeFileSync(inputPath, buffer); |
| 94 | + |
| 95 | + const inputFile = fs.readFileSync(inputPath); |
| 96 | + // Convert to PDF using LibreOffice |
| 97 | + const pdfBuffer = await convert(inputFile, ".pdf", undefined); |
| 98 | + fs.writeFileSync(outputPath, pdfBuffer); |
| 99 | + |
| 100 | + // Upload to R2 |
| 101 | + const key = `converted-pdfs/output_${Date.now()}.pdf`; |
| 102 | + await s3Client.send( |
| 103 | + new PutObjectCommand({ |
| 104 | + Bucket: process.env.R2_BUCKET, |
| 105 | + Key: key, |
| 106 | + Body: fs.readFileSync(outputPath), |
| 107 | + }) |
| 108 | + ); |
| 109 | + |
| 110 | + // Cleanup temporary files |
| 111 | + fs.unlinkSync(inputPath); |
| 112 | + fs.unlinkSync(outputPath); |
| 113 | + |
| 114 | + return { pdfLocation: key }; |
| 115 | + } catch (error) { |
| 116 | + console.error("Error converting PDF:", error); |
| 117 | + throw error; |
| 118 | + } |
| 119 | + }, |
| 120 | +}); |
| 121 | +``` |
| 122 | + |
| 123 | +### Testing your task |
| 124 | + |
| 125 | +To test this task, use this payload structure: |
| 126 | + |
| 127 | +```json |
| 128 | +{ |
| 129 | + "documentUrl": "<a-document-url>" // Replace <a-document-url> with the URL of the document you want to convert |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +<LocalDevelopment packages={"libreoffice"} /> |
0 commit comments