diff --git a/src/converters/libreoffice.ts b/src/converters/libreoffice.ts new file mode 100644 index 0000000..87441e0 --- /dev/null +++ b/src/converters/libreoffice.ts @@ -0,0 +1,45 @@ +import { execFile } from "node:child_process"; + +export const properties = { + from: { + text: ["docx", "txt"], + }, + to: { + text: ["pdf", "txt"], + }, +}; + +export function convert( + filePath: string, + fileType: string, + convertTo: string, + targetPath: string, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + options?: unknown, +): Promise { + const outputPath = targetPath.split("/").slice(0, -1).join("/").replace("./", ""); + // Build arguments array + const args: string[] = []; + + args.push("--headless"); + args.push("--convert-to", convertTo, filePath); + args.push("--outdir", outputPath); + + return new Promise((resolve, reject) => { + execFile("soffice", args, (error, stdout, stderr) => { + if (error) { + reject(`error: ${error}`); + } + + if (stdout) { + console.log(`stdout: ${stdout}`); + } + + if (stderr) { + console.error(`stderr: ${stderr}`); + } + + resolve("Done"); + }); + }); +} diff --git a/src/converters/main.ts b/src/converters/main.ts index a9f2e42..c0d60f1 100644 --- a/src/converters/main.ts +++ b/src/converters/main.ts @@ -11,6 +11,7 @@ import { convert as convertImagemagick, properties as propertiesImagemagick } fr import { convert as convertInkscape, properties as propertiesInkscape } from "./inkscape"; import { convert as convertLibheif, properties as propertiesLibheif } from "./libheif"; import { convert as convertLibjxl, properties as propertiesLibjxl } from "./libjxl"; +import { convert as convertLibreOffice, properties as propertiesLibreOffice } from "./libreoffice"; import { convert as convertPandoc, properties as propertiesPandoc } from "./pandoc"; import { convert as convertPotrace, properties as propertiesPotrace } from "./potrace"; import { convert as convertresvg, properties as propertiesresvg } from "./resvg"; @@ -71,6 +72,10 @@ const properties: Record< properties: propertiesCalibre, converter: convertCalibre, }, + libreoffice: { + properties: propertiesLibreOffice, + converter: convertLibreOffice, + }, pandoc: { properties: propertiesPandoc, converter: convertPandoc, diff --git a/src/helpers/printVersions.ts b/src/helpers/printVersions.ts index 42044d2..68af2e0 100644 --- a/src/helpers/printVersions.ts +++ b/src/helpers/printVersions.ts @@ -144,6 +144,16 @@ if (process.env.NODE_ENV === "production") { } }); + exec("soffice --version", (error, stdout) => { + if (error) { + console.error("libreoffice is not installed"); + } + + if (stdout) { + console.log(stdout.split("\n")[0]); + } + }); + exec("bun -v", (error, stdout) => { if (error) { console.error("Bun is not installed. wait what");