//:path=App.javaimport com.amazonaws.services.lambda.runtime.Context;import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;import com.ironsoftware.ironpdf.PdfDocument;import com.ironsoftware.ironpdf.Settings;import java.nio.file.Paths;import java.util.HashMap;import java.util.Map;public class App { public APIGatewayProxyResponseEvent handleRequest( final APIGatewayProxyRequestEvent input, final Context context) { APIGatewayProxyResponseEvent response = newAPIGatewayProxyResponseEvent(); // IronPDF must write its engine binaries and temporary files to /tmp. // This is the only writable path available in the Lambda execution environment.Settings.setIronPdfEngineWorkingDirectory(Paths.get("/tmp/")); // Enable debug logging to CloudWatch during initial testing.Settings.setDebug(true); try { context.getLogger().log("Starting PDF render"); // Render a PDF from a live URL. Replace with your own HTML or URL as needed. PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://www.google.com"); context.getLogger().log("PDF render complete"); // Save the rendered PDF to /tmp. Files in /tmp persist for the lifetime // of the Lambda execution environment (warm instance). pdf.saveAs("/tmp/output.pdf"); Map<String, String> headers = new HashMap<>(); headers.put("Content-Type", "application/json"); return response .withStatusCode(200) .withHeaders(headers) .withBody("PDF generated successfully."); } catch (Exception e) { context.getLogger().log("PDF render failed: " + e.getMessage()); return response .withStatusCode(500) .withBody("{\"error\": \"" + e.getMessage() + "\"}"); } }}
//:path=App.java
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.Settings;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
public class App {
public APIGatewayProxyResponseEvent handleRequest(
final APIGatewayProxyRequestEvent input,
final Context context) {
APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();
// IronPDF must write its engine binaries and temporary files to /tmp.
// This is the only writable path available in the Lambda execution environment.
Settings.setIronPdfEngineWorkingDirectory(Paths.get("/tmp/"));
// Enable debug logging to CloudWatch during initial testing.
Settings.setDebug(true);
try {
context.getLogger().log("Starting PDF render");
// Render a PDF from a live URL. Replace with your own HTML or URL as needed.
PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://www.google.com");
context.getLogger().log("PDF render complete");
// Save the rendered PDF to /tmp. Files in /tmp persist for the lifetime
// of the Lambda execution environment (warm instance).
pdf.saveAs("/tmp/output.pdf");
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
return response
.withStatusCode(200)
.withHeaders(headers)
.withBody("PDF generated successfully.");
} catch (Exception e) {
context.getLogger().log("PDF render failed: " + e.getMessage());
return response
.withStatusCode(500)
.withBody("{\"error\": \"" + e.getMessage() + "\"}");
}
}
}
Dockerfile是此部署的核心。 它執行多步驟構建:第一步驟使用Maven構建映像編譯Java項目; 第二步驟基於Amazon Linux 2建立最終的Lambda運行時映像,安裝IronPDF的Chromium引擎所需的系統包,並複製已編譯的工件。
打開專案的Dockerfile並將其內容替換為以下內容:
//:path=Dockerfile# Stage 1: Build the Maven projectFROM public.ecr.aws/sam/build-java8.al2:latest AS build-imageWORKDIR /taskCOPY src/src/COPY pom.xml ./RUN mvn -q clean installRUN mvn dependency:copy-dependencies -DincludeScope=compile# Stage 2: Create the Lambda runtime imageFROM public.ecr.aws/lambda/java:8.al2# Update the package index and install system libraries required by Chromium.# These packages provide font rendering, graphics, audio, GTK3, and input# method support — all needed by the headless browser inside IronPDF.RUN yum update -y && \ yum install -y \ pango.x86_64 \ libXcomposite.x86_64 \ libXcursor.x86_64 \ libXdamage.x86_64 \ libXext.x86_64 \ libXi.x86_64 \ libXtst.x86_64 \ cups-libs.x86_64 \ libXScrnSaver.x86_64 \ libXrandr.x86_64 \ GConf2.x86_64 \ alsa-lib.x86_64 \ atk.x86_64 \ gtk3.x86_64 \ ipa-gothic-fonts \ xorg-x11-fonts-100dpi \ xorg-x11-fonts-75dpi \ xorg-x11-utils \ xorg-x11-fonts-cyrillic \ xorg-x11-fonts-Type1 \ xorg-x11-fonts-misc \ glibc-devel.x86_64 \ at-spi2-atk.x86_64 \ mesa-libgbm.x86_64 \ libxkbcommon \ amazon-linux-extras && \ amazon-linux-extras install epel -y && \ yum install -y libgdiplus# Ensure /tmp is writable by the Lambda execution user.RUN chmod 777 /tmp/# Copy the compiled classes and dependencies from the build stage.COPY --from=build-image /task/target/classes /var/task/COPY --from=build-image /task/target/dependency /var/task/lib# Entry point: package.ClassName::methodNameCMD ["helloworld.App::handleRequest"]
//:path=Dockerfile
# Stage 1: Build the Maven project
FROM public.ecr.aws/sam/build-java8.al2:latest AS build-image
WORKDIR /task
COPY src/src/
COPY pom.xml ./
RUN mvn -q clean install
RUN mvn dependency:copy-dependencies -DincludeScope=compile
# Stage 2: Create the Lambda runtime image
FROM public.ecr.aws/lambda/java:8.al2
# Update the package index and install system libraries required by Chromium.
# These packages provide font rendering, graphics, audio, GTK3, and input
# method support — all needed by the headless browser inside IronPDF.
RUN yum update -y && \
yum install -y \
pango.x86_64 \
libXcomposite.x86_64 \
libXcursor.x86_64 \
libXdamage.x86_64 \
libXext.x86_64 \
libXi.x86_64 \
libXtst.x86_64 \
cups-libs.x86_64 \
libXScrnSaver.x86_64 \
libXrandr.x86_64 \
GConf2.x86_64 \
alsa-lib.x86_64 \
atk.x86_64 \
gtk3.x86_64 \
ipa-gothic-fonts \
xorg-x11-fonts-100dpi \
xorg-x11-fonts-75dpi \
xorg-x11-utils \
xorg-x11-fonts-cyrillic \
xorg-x11-fonts-Type1 \
xorg-x11-fonts-misc \
glibc-devel.x86_64 \
at-spi2-atk.x86_64 \
mesa-libgbm.x86_64 \
libxkbcommon \
amazon-linux-extras && \
amazon-linux-extras install epel -y && \
yum install -y libgdiplus
# Ensure /tmp is writable by the Lambda execution user.
RUN chmod 777 /tmp/
# Copy the compiled classes and dependencies from the build stage.
COPY --from=build-image /task/target/classes /var/task/
COPY --from=build-image /task/target/dependency /var/task/lib
# Entry point: package.ClassName::methodName
CMD ["helloworld.App::handleRequest"]
Text
兩個階段的基礎映像都使用java8。 .al2後綴表示Amazon Linux 2,IronPDF需要它。 舊的java8映像在原始的Amazon Linux 1上運行,使用的是不再接收更新且缺少IronPDF所依賴的幾個包的yum儲存庫。 在Java 8上部署IronPDF時,始終使用.al2映像。