Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

Bundle the form detection engine and model in the backend image (SaaS) (#8058)

Anthony Stirlingd2d7788dd3a5
diff --git a/docker/backend/Dockerfile b/docker/backend/Dockerfile
index 9a1c5c836e..cd196878b6 100644
--- a/docker/backend/Dockerfile
+++ b/docker/backend/Dockerfile
@@ -6,6 +6,16 @@ ARG BASE_IMAGE=stirlingtools/stirling-pdf-base:${BASE_VERSION}
# Stage 1: Build the Java application (backend only, no frontend)
FROM gradle:9.7.1-jdk25@sha256:eec96c1a66c66235f8d58d4fdb3ee3b9c52852d895001cc3183388fbf421ff42 AS app-build
+RUN apt-get update \
+ && apt-get install -y --no-install-recommends curl ca-certificates \
+ && update-ca-certificates \
+ && rm -rf /var/lib/apt/lists/* \
+ && mkdir -p /preinstalled-models \
+ && curl -fSL "https://huggingface.co/Frooodle/ffdetr-int8/resolve/ad3223d7c3ae25ce1d1ec8fe972c8ff7630aff45/ffdetr-int8.onnx" \
+ -o /preinstalled-models/ffdetr.onnx \
+ && echo "de43bb39adb08459fe62fa1a7be6bfb4f2d397bb034c46055f3b8fa3d04fc8f4 /preinstalled-models/ffdetr.onnx" \
+ | sha256sum -c -
+
# JDK 25+: --add-exports is no longer accepted via JAVA_TOOL_OPTIONS; use JDK_JAVA_OPTIONS instead
ENV JDK_JAVA_OPTIONS="--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
@@ -41,6 +51,7 @@ RUN JPDFIUM_PLATFORM="$([ "$TARGETARCH" = arm64 ] && echo linux-arm64 || echo li
-PbuildWithFrontend=false \
-PjpdfiumPlatforms="$JPDFIUM_PLATFORM" \
-PprototypesMode=${PROTOTYPES_BUILD} \
+ -PbundleOnnxRuntime=true \
-x spotlessApply -x spotlessCheck -x test -x sonarqube \
--no-daemon
@@ -50,6 +61,12 @@ WORKDIR /tmp
COPY --from=app-build /app/app/core/build/libs/*.jar app.jar
RUN java -Djarmode=tools -jar app.jar extract --layers --destination /layers
+COPY scripts/slim-onnxruntime.sh /tmp/slim-onnxruntime.sh
+RUN apt-get update \
+ && apt-get install -y --no-install-recommends zip \
+ && rm -rf /var/lib/apt/lists/* \
+ && sh /tmp/slim-onnxruntime.sh /layers/dependencies
+
# Stage 3: Final runtime image on top of pre-built base
FROM ${BASE_IMAGE}
@@ -68,6 +85,8 @@ COPY --link --from=app-build --chown=1000:1000 \
/app/build/libs/restart-helper.jar /restart-helper.jar
COPY --link --chown=1000:1000 scripts/ /scripts/
+COPY --link --from=app-build --chown=1000:1000 /preinstalled-models/ /opt/stirling/preinstalled-models/
+
# Fonts go to system dir, root ownership is correct (world-readable)
COPY app/core/src/main/resources/static/fonts/*.ttf /usr/share/fonts/truetype/
@@ -90,6 +109,7 @@ RUN echo "${VERSION_TAG:-dev}" > /etc/stirling_version
# Environment variables
ENV VERSION_TAG=$VERSION_TAG \
+ FORMDETECTION_PREINSTALLEDMODELDIR="/opt/stirling/preinstalled-models" \
STIRLING_AOT_ENABLE="false" \
STIRLING_JVM_PROFILE="balanced" \
_JVM_OPTS_BALANCED="-XX:+ExitOnOutOfMemoryError -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/configs/heap_dumps -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:G1HeapRegionSize=4m -XX:G1PeriodicGCInterval=60000 -XX:+UseStringDeduplication -XX:+UseCompactObjectHeaders -XX:+ExplicitGCInvokesConcurrent -Dspring.threads.virtual.enabled=true -Djava.awt.headless=true" \