From 9c7787cf93da26398119a3eee77329f22d9d892e Mon Sep 17 00:00:00 2001 From: D-K-P <8297864+D-K-P@users.noreply.github.com> Date: Wed, 13 Nov 2024 14:54:36 +0000 Subject: [PATCH 1/2] Added Libreoffice example docs --- .../examples/libreoffice-pdf-conversion.mdx | 133 ++++++++++++++++++ docs/guides/introduction.mdx | 3 +- docs/mint.json | 1 + 3 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 docs/guides/examples/libreoffice-pdf-conversion.mdx diff --git a/docs/guides/examples/libreoffice-pdf-conversion.mdx b/docs/guides/examples/libreoffice-pdf-conversion.mdx new file mode 100644 index 0000000000..af304d261a --- /dev/null +++ b/docs/guides/examples/libreoffice-pdf-conversion.mdx @@ -0,0 +1,133 @@ +--- +title: "Convert documents to PDF using LibreOffice" +sidebarTitle: "LibreOffice PDF conversion" +description: "This example demonstrates how to convert documents to PDF using LibreOffice with Trigger.dev." +--- + +import LocalDevelopment from "/snippets/local-development-extensions.mdx"; + +## Prerequisites + +- A project with [Trigger.dev initialized](/quick-start) +- [LibreOffice](https://www.libreoffice.org/download/libreoffice-fresh/) installed on your machine +- A [Cloudflare R2](https://developers.cloudflare.com) account and bucket + +### Using our `aptGet` build extension to add the LibreOffice package + +To deploy this task, you'll need to add LibreOffice to your project configuration, like this: + +```ts trigger.config.ts +import { aptGet } from "@trigger.dev/build/extensions/core"; +import { defineConfig } from "@trigger.dev/sdk/v3"; + +export default defineConfig({ + project: "", + // Your other config settings... + build: { + extensions: [ + aptGet({ + packages: ["libreoffice"], + }), + ], + }, +}); +``` + + + [Build extensions](/config/config-file#extensions) allow you to hook into the build system and + customize the build process or the resulting bundle and container image (in the case of + deploying). You can use pre-built extensions or create your own. + + +You'll also need to add `@trigger.dev/build` to your `package.json` file under `devDependencies` if you don't already have it there. + +## Convert a document to PDF using LibreOffice and upload to R2 + +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. + +### Key Features + +- Fetches a document from a given URL +- Converts the document to PDF +- Uploads the PDF to R2 storage + +### Task code + +```ts trigger/libreoffice-pdf-convert.ts +import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3"; +import { task } from "@trigger.dev/sdk/v3"; +import libreoffice from "libreoffice-convert"; +import { promisify } from "node:util"; +import path from "path"; +import fs from "fs"; + +const convert = promisify(libreoffice.convert); + +// Initialize S3 client +const s3Client = new S3Client({ + // How to authenticate to R2: https://developers.cloudflare.com/r2/api/s3/tokens/ + region: "auto", + endpoint: process.env.R2_ENDPOINT, + credentials: { + accessKeyId: process.env.R2_ACCESS_KEY_ID ?? "", + secretAccessKey: process.env.R2_SECRET_ACCESS_KEY ?? "", + }, +}); + +export const libreOfficePdfConvert = task({ + id: "libreoffice-pdf-convert", + run: async (payload: { documentUrl: string }, { ctx }) => { + // Set LibreOffice path for production environment + if (ctx.environment.type !== "DEVELOPMENT") { + process.env.LIBREOFFICE_PATH = "/usr/bin/libreoffice"; + } + + try { + // Create temporary file paths + const inputPath = path.join(process.cwd(), `input_${Date.now()}.docx`); + const outputPath = path.join(process.cwd(), `output_${Date.now()}.pdf`); + + // Download file from URL + const response = await fetch(payload.documentUrl); + const buffer = Buffer.from(await response.arrayBuffer()); + fs.writeFileSync(inputPath, buffer); + + const inputFile = fs.readFileSync(inputPath); + // Convert to PDF using LibreOffice + const pdfBuffer = await convert(inputFile, ".pdf", undefined); + fs.writeFileSync(outputPath, pdfBuffer); + + // Upload to R2 + const key = `converted-pdfs/output_${Date.now()}.pdf`; + await s3Client.send( + new PutObjectCommand({ + Bucket: process.env.R2_BUCKET, + Key: key, + Body: fs.readFileSync(outputPath), + }) + ); + + // Cleanup temporary files + fs.unlinkSync(inputPath); + fs.unlinkSync(outputPath); + + return { pdfLocation: key }; + } catch (error) { + console.error("Error converting PDF:", error); + throw error; + } + }, +}); +``` + +### Testing your task + +To test this task, use this payload structure: + +```json +{ + "documentUrl": "" // Replace with the URL of the document you want to convert +} +``` + + diff --git a/docs/guides/introduction.mdx b/docs/guides/introduction.mdx index a3c2499514..51d58b0a49 100644 --- a/docs/guides/introduction.mdx +++ b/docs/guides/introduction.mdx @@ -42,9 +42,10 @@ Tasks you can copy and paste to get started with Trigger.dev. They can all be ex | [DALL·E 3 image generation](/guides/examples/dall-e3-generate-image) | Use OpenAI's GPT-4o and DALL·E 3 to generate an image and text. | | [Deepgram audio transcription](/guides/examples/deepgram-transcribe-audio) | Transcribe audio using Deepgram's speech recognition API. | | [Fal.ai image to cartoon](/guides/examples/fal-ai-image-to-cartoon) | Convert an image to a cartoon using Fal.ai, and upload the result to Cloudflare R2. | -| [Fal.ai with Realtime](/guides/examples/fal-ai-realtime) | Generate an image from a prompt using Fal.ai and show the progress of the task on the frontend using Realtime. | +| [Fal.ai with Realtime](/guides/examples/fal-ai-realtime) | Generate an image from a prompt using Fal.ai and show the progress of the task on the frontend using Realtime. | | [FFmpeg video processing](/guides/examples/ffmpeg-video-processing) | Use FFmpeg to process a video in various ways and save it to Cloudflare R2. | | [Firecrawl URL crawl](/guides/examples/firecrawl-url-crawl) | Learn how to use Firecrawl to crawl a URL and return LLM-ready markdown. | +| [LibreOffice PDF conversion](/guides/examples/libreoffice-pdf-conversion) | Convert a document to PDF using LibreOffice. | | [OpenAI with retrying](/guides/examples/open-ai-with-retrying) | Create a reusable OpenAI task with custom retry options. | | [PDF to image](/guides/examples/pdf-to-image) | Use `MuPDF` to turn a PDF into images and save them to Cloudflare R2. | | [React to PDF](/guides/examples/react-pdf) | Use `react-pdf` to generate a PDF and save it to Cloudflare R2. | diff --git a/docs/mint.json b/docs/mint.json index 364ff188e1..23c55f6b99 100644 --- a/docs/mint.json +++ b/docs/mint.json @@ -340,6 +340,7 @@ "guides/examples/fal-ai-realtime", "guides/examples/ffmpeg-video-processing", "guides/examples/firecrawl-url-crawl", + "guides/examples/libreoffice-pdf-conversion", "guides/examples/open-ai-with-retrying", "guides/examples/pdf-to-image", "guides/examples/puppeteer", From 7e15513e9dc0370c19cc895b312c37a723d95b8c Mon Sep 17 00:00:00 2001 From: D-K-P <8297864+D-K-P@users.noreply.github.com> Date: Wed, 13 Nov 2024 15:01:50 +0000 Subject: [PATCH 2/2] typo --- docs/guides/examples/libreoffice-pdf-conversion.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/guides/examples/libreoffice-pdf-conversion.mdx b/docs/guides/examples/libreoffice-pdf-conversion.mdx index af304d261a..50f4299f68 100644 --- a/docs/guides/examples/libreoffice-pdf-conversion.mdx +++ b/docs/guides/examples/libreoffice-pdf-conversion.mdx @@ -126,7 +126,7 @@ To test this task, use this payload structure: ```json { - "documentUrl": "" // Replace with the URL of the document you want to convert + "documentUrl": "" // Replace with the URL of the document you want to convert } ```