diff --git a/Dockerfile b/Dockerfile index 33f7cc91..95cec06d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,102 +1,120 @@ -FROM oven/bun:1.4.5-slim AS base -WORKDIR /app -COPY package.json bun.lock /app/ -RUN bun install --frozen-lockfile -COPY . . - -RUN bun run build:bin - -FROM debian:testing-slim AS release -LABEL org.opencontainers.image.source="https://github.com/C4illin/ConvertX" -WORKDIR /app - -# install additional dependencies -ENV PIPX_HOME=/opt/pipx -ENV PIPX_BIN_DIR=/usr/local/bin - -RUN apt-get update && apt-get install -y \ - assimp-utils \ - ca-certificates \ - calibre \ - curl \ - dasel \ - dcraw \ - djvulibre-bin \ - dvisvgm \ - ffmpeg \ - ghostscript \ - gosu \ - graphicsmagick \ - imagemagick-7.q16 \ - inkscape \ - latexmk \ - libheif-examples \ - libjxl-tools \ - libreoffice-calc-nogui \ - libreoffice-draw-nogui \ - libreoffice-writer-nogui \ - libva2 \ - libvips-tools \ - libemail-address-perl \ - libemail-outlook-message-perl \ - lmodern \ - mupdf-tools \ - pandoc \ - poppler-utils \ - potrace \ - python3-numpy \ - python3-tinycss2 \ - resvg \ - texlive \ - texlive-fonts-recommended \ - texlive-latex-extra \ - texlive-latex-recommended \ - texlive-xetex \ - python3 \ - python3-pip \ - pipx \ - --no-install-recommends \ - && pipx install "markitdown[all]" \ - && rm -rf /var/lib/apt/lists/* - -# Remove when patch is released -RUN pipx runpip markitdown install --upgrade --no-cache-dir "Brotli>=1.2.1" "brotlicffi>=1.2.1.0" - -RUN groupadd -g 1000 convertx && \ - useradd -u 1000 -g convertx -d /home/convertx -m -s /bin/bash convertx - -# Install VTracer binary -RUN ARCH=$(uname -m) && \ - if [ "$ARCH" = "aarch64" ]; then \ - VTRACER_ASSET="vtracer-aarch64-unknown-linux-musl.tar.gz"; \ - else \ - VTRACER_ASSET="vtracer-x86_64-unknown-linux-musl.tar.gz"; \ - fi && \ - curl -L -o /tmp/vtracer.tar.gz "https://github.com/visioncortex/vtracer/releases/download/0.6.4/${VTRACER_ASSET}" && \ - tar -xzf /tmp/vtracer.tar.gz -C /tmp/ && \ - mv /tmp/vtracer /usr/local/bin/vtracer && \ - chmod +x /usr/local/bin/vtracer && \ - rm /tmp/vtracer.tar.gz - -COPY --from=base /app/public/ /app/public/ -COPY --from=base /app/dist/convertx /app/convertx - -# COPY . . -RUN mkdir data - -COPY entrypoint.sh /entrypoint.sh -RUN chmod +x /entrypoint.sh - -EXPOSE 3000/tcp -# used for calibre -ENV QTWEBENGINE_CHROMIUM_FLAGS="--no-sandbox" -ENV NODE_ENV=production -ENTRYPOINT [ "/entrypoint.sh" ] -CMD [ "/app/convertx" ] - -# test stage that includes Bun for running tests if needed -FROM release AS test -COPY --from=base /usr/local/bin/bun /usr/local/bin/bun - -# default target remains release -FROM release +FROM oven/bun:1.4.5-slim AS base +WORKDIR /app +COPY package.json bun.lock /app/ +RUN bun install --frozen-lockfile +COPY . . + +RUN bun run build:bin + +FROM debian:testing-slim AS release +LABEL org.opencontainers.image.source="https://github.com/C4illin/ConvertX" +WORKDIR /app + +# install additional dependencies +ENV PIPX_HOME=/opt/pipx +ENV PIPX_BIN_DIR=/usr/local/bin + +RUN apt-get update && apt-get install -y \ + assimp-utils \ + ca-certificates \ + calibre \ + curl \ + dasel \ + dcraw \ + djvulibre-bin \ + dvisvgm \ + ffmpeg \ + ghostscript \ + gosu \ + graphicsmagick \ + imagemagick-7.q16 \ + inkscape \ + latexmk \ + libheif-examples \ + libjxl-tools \ + libreoffice-calc-nogui \ + libreoffice-draw-nogui \ + libreoffice-writer-nogui \ + libva2 \ + libvips-tools \ + libemail-address-perl \ + libemail-outlook-message-perl \ + lmodern \ + mupdf-tools \ + pandoc \ + poppler-utils \ + potrace \ + python3-numpy \ + python3-tinycss2 \ + resvg \ + texlive \ + texlive-fonts-recommended \ + texlive-latex-extra \ + texlive-latex-recommended \ + texlive-xetex \ + python3 \ + python3-pip \ + pipx \ + --no-install-recommends \ + && pipx install "markitdown[all]" \ + && rm -rf /var/lib/apt/lists/* + +# Remove when patch is released +RUN pipx runpip markitdown install --upgrade --no-cache-dir "Brotli>=1.2.1" "brotlicffi>=1.2.1.0" + +RUN groupadd -g 1000 convertx && \ + useradd -u 1000 -g convertx -d /home/convertx -m -s /bin/bash convertx + +# Install VTracer binary +RUN ARCH=$(uname -m) && \ + if [ "$ARCH" = "aarch64" ]; then \ + VTRACER_ASSET="vtracer-aarch64-unknown-linux-musl.tar.gz"; \ + else \ + VTRACER_ASSET="vtracer-x86_64-unknown-linux-musl.tar.gz"; \ + fi && \ + curl -L -o /tmp/vtracer.tar.gz "https://github.com/visioncortex/vtracer/releases/download/0.6.4/${VTRACER_ASSET}" && \ + tar -xzf /tmp/vtracer.tar.gz -C /tmp/ && \ + mv /tmp/vtracer /usr/local/bin/vtracer && \ + chmod +x /usr/local/bin/vtracer && \ + rm /tmp/vtracer.tar.gz + +# Install oxidocs binary (docx, pptx and xlsx to pdf/png). It writes the fonts it +# carries next to itself on first use, so that is done here, as root: with PUID +# set, /usr/local/bin is not writable when it runs. +# renovate: datasource=github-releases depName=Ryujiyasu/oxi versioning=regex:^(?\d+)\.(?\d+)\.(?\d+)(?:-static\.(?\d+))?$ extractVersion=^oxidocs-v(?.+)$ +ARG OXIDOCS_VERSION=0.9.0-static.6 +RUN ARCH=$(uname -m) && \ + if [ "$ARCH" = "aarch64" ]; then \ + OXIDOCS_ASSET="oxidocs-aarch64-unknown-linux-musl"; \ + else \ + OXIDOCS_ASSET="oxidocs-x86_64-unknown-linux-musl"; \ + fi && \ + curl -fL -o /tmp/oxidocs.tar.gz "https://github.com/Ryujiyasu/oxi/releases/download/oxidocs-v${OXIDOCS_VERSION}/${OXIDOCS_ASSET}.tar.gz" && \ + tar -xzf /tmp/oxidocs.tar.gz -C /tmp/ && \ + mv "/tmp/${OXIDOCS_ASSET}/oxidocs" /usr/local/bin/oxidocs && \ + chmod +x /usr/local/bin/oxidocs && \ + oxidocs fonts-index > /dev/null && \ + rm -r /tmp/oxidocs.tar.gz "/tmp/${OXIDOCS_ASSET}" + +COPY --from=base /app/public/ /app/public/ +COPY --from=base /app/dist/convertx /app/convertx + +# COPY . . +RUN mkdir data + +COPY entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh + +EXPOSE 3000/tcp +# used for calibre +ENV QTWEBENGINE_CHROMIUM_FLAGS="--no-sandbox" +ENV NODE_ENV=production +ENTRYPOINT [ "/entrypoint.sh" ] +CMD [ "/app/convertx" ] + +# test stage that includes Bun for running tests if needed +FROM release AS test +COPY --from=base /usr/local/bin/bun /usr/local/bin/bun + +# default target remains release +FROM release diff --git a/README.md b/README.md index 6ec13378..f5f9a83d 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ A self-hosted online file converter. Supports over a thousand different formats. | [XeLaTeX](https://tug.org/xetex/) | LaTeX | 1 | 1 | | [Calibre](https://calibre-ebook.com/) | E-books | 26 | 19 | | [LibreOffice](https://www.libreoffice.org/) | Documents | 41 | 22 | +| [Oxi](https://github.com/Ryujiyasu/oxi) | Word documents | 3 | 2 | | [Dasel](https://github.com/TomWright/dasel) | Data Files | 5 | 4 | | [DjVuLibre](https://djvu.sourceforge.net/) | Documents | 2 | 2 | | [Pandoc](https://pandoc.org/) | Documents | 43 | 65 | diff --git a/knip.json b/knip.json index c9e38040..9c9e9477 100644 --- a/knip.json +++ b/knip.json @@ -18,6 +18,7 @@ "heif-info", "potrace", "soffice", + "oxidocs", "perl", "markitdown" ], diff --git a/renovate.json b/renovate.json index 3418a449..30d39593 100644 --- a/renovate.json +++ b/renovate.json @@ -1,6 +1,10 @@ { "$schema": "https://docs.renovatebot.com/renovate-schema.json", - "extends": ["config:recommended", ":disableDependencyDashboard"], + "extends": [ + "config:recommended", + ":disableDependencyDashboard", + "customManagers:dockerfileVersions" + ], "lockFileMaintenance": { "enabled": true, "automerge": true diff --git a/src/converters/main.ts b/src/converters/main.ts index 4ba2af52..4d373c16 100644 --- a/src/converters/main.ts +++ b/src/converters/main.ts @@ -27,6 +27,7 @@ import { convert as convertLibjxl, properties as propertiesLibjxl } from "./libj import { convert as convertLibreOffice, properties as propertiesLibreOffice } from "./libreoffice"; import { convert as convertMarkitdown, properties as propertiesMarkitdown } from "./markitdown"; import { convert as convertMsgconvert, properties as propertiesMsgconvert } from "./msgconvert"; +import { convert as convertOxidocs, properties as propertiesOxidocs } from "./oxidocs"; import { convert as convertPandoc, properties as propertiesPandoc } from "./pandoc"; import { convert as convertPdftops, properties as propertiesPdftops } from "./pdftops"; import { convert as convertPotrace, properties as propertiesPotrace } from "./potrace"; @@ -108,6 +109,10 @@ const properties: Record< properties: propertiesLibreOffice, converter: convertLibreOffice, }, + oxidocs: { + properties: propertiesOxidocs, + converter: convertOxidocs, + }, pandoc: { properties: propertiesPandoc, converter: convertPandoc, diff --git a/src/converters/oxidocs.ts b/src/converters/oxidocs.ts new file mode 100644 index 00000000..1c72916d --- /dev/null +++ b/src/converters/oxidocs.ts @@ -0,0 +1,60 @@ +import { execFile as execFileOriginal } from "node:child_process"; + +import { ExecFileFn } from "./types"; + +export const properties = { + from: { + text: ["docx", "docm", "dotx"], + presentation: ["pptx", "pptm", "potx"], + calc: ["xlsx", "xlsm", "xltx", "xltm"], + }, + to: { + text: ["pdf", "png"], + presentation: ["pdf", "png"], + calc: ["pdf", "png"], + }, +}; + +const presentations = new Set(properties.from.presentation); +const workbooks = new Set(properties.from.calc); + +export function convert( + filePath: string, + fileType: string, + convertTo: string, + targetPath: string, + options?: unknown, + execFile: ExecFileFn = execFileOriginal, // to make it mockable +): Promise { + // png draws the first page (the first slide, the first printed page of a + // workbook); pdf converts the whole + // document, every slide, or the print areas of every visible sheet. + // --strict: when the document holds content Oxi does not draw, it writes + // nothing and fails (naming the content) rather than hand back a file with + // parts missing; LibreOffice can then be chosen instead. + // --quiet: nothing is printed on success, only warnings and errors + const kind = presentations.has(fileType) ? "pptx" : workbooks.has(fileType) ? "xlsx" : "docx"; + const command = `${kind}-to-${convertTo === "png" ? "png" : "pdf"}`; + + return new Promise((resolve, reject) => { + execFile( + "oxidocs", + [command, "--strict", "--quiet", filePath, targetPath], + (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/helpers/printVersions.ts b/src/helpers/printVersions.ts index 6d7ba3b4..97171ff6 100644 --- a/src/helpers/printVersions.ts +++ b/src/helpers/printVersions.ts @@ -106,6 +106,17 @@ if (process.env.NODE_ENV === "production") { } }); + exec("oxidocs --version", (error, stdout) => { + if (error) { + console.error("oxidocs is not installed"); + return; + } + + if (stdout) { + console.log(`oxidocs v${stdout.replace("oxidocs", "").trim()}`); + } + }); + exec("xelatex -version", (error, stdout) => { if (error) { console.error("Tex Live with XeTeX is not installed."); diff --git a/tests/converters/main.test.ts b/tests/converters/main.test.ts index d663d87b..13438f0c 100644 --- a/tests/converters/main.test.ts +++ b/tests/converters/main.test.ts @@ -125,6 +125,7 @@ describe("converter registry", () => { "libreoffice", "markitDown", "msgconvert", + "oxidocs", "pandoc", "pdftops", "potrace", diff --git a/tests/converters/oxidocs.test.ts b/tests/converters/oxidocs.test.ts new file mode 100644 index 00000000..72adc8a6 --- /dev/null +++ b/tests/converters/oxidocs.test.ts @@ -0,0 +1,69 @@ +import { expect, test } from "bun:test"; + +import { convert } from "../../src/converters/oxidocs"; +import { runCommonTests } from "./helpers/commonTests"; +import { createMockExecFile } from "./helpers/converters"; + +runCommonTests(convert); + +test("converts docx to pdf with docx-to-pdf", async () => { + const { execFile, calls } = createMockExecFile(); + + await convert("in/report.docx", "docx", "pdf", "out/report.pdf", undefined, execFile); + + expect(calls).toEqual([ + { + cmd: "oxidocs", + args: ["docx-to-pdf", "--strict", "--quiet", "in/report.docx", "out/report.pdf"], + }, + ]); +}); + +test("draws the first page with docx-to-png", async () => { + const { execFile, calls } = createMockExecFile(); + + await convert("in/report.docx", "docx", "png", "out/report.png", undefined, execFile); + + expect(calls).toEqual([ + { + cmd: "oxidocs", + args: ["docx-to-png", "--strict", "--quiet", "in/report.docx", "out/report.png"], + }, + ]); +}); + +test("converts a presentation with pptx-to-pdf and pptx-to-png", async () => { + const { execFile, calls } = createMockExecFile(); + + await convert("in/deck.pptx", "pptx", "pdf", "out/deck.pdf", undefined, execFile); + await convert("in/deck.pptx", "pptx", "png", "out/deck.png", undefined, execFile); + + expect(calls).toEqual([ + { + cmd: "oxidocs", + args: ["pptx-to-pdf", "--strict", "--quiet", "in/deck.pptx", "out/deck.pdf"], + }, + { + cmd: "oxidocs", + args: ["pptx-to-png", "--strict", "--quiet", "in/deck.pptx", "out/deck.png"], + }, + ]); +}); + +test("converts a workbook with xlsx-to-pdf and xlsx-to-png", async () => { + const { execFile, calls } = createMockExecFile(); + + await convert("in/sheet.xlsx", "xlsx", "pdf", "out/sheet.pdf", undefined, execFile); + await convert("in/sheet.xlsx", "xlsx", "png", "out/sheet.png", undefined, execFile); + + expect(calls).toEqual([ + { + cmd: "oxidocs", + args: ["xlsx-to-pdf", "--strict", "--quiet", "in/sheet.xlsx", "out/sheet.pdf"], + }, + { + cmd: "oxidocs", + args: ["xlsx-to-png", "--strict", "--quiet", "in/sheet.xlsx", "out/sheet.png"], + }, + ]); +});