如何在 AWS Lambda 中运行 IronPDF for Java.

This article was translated from English: Does it need improvement?
Translated
View the article in English

重要:必要设置

  • 由于 IronPDF 需要在运行时执行二进制文件,因此不支持 Zip 部署
  • 您必须将 PackageType 设置为 Image ,因为 IronPDF for Java 仅支持 Docker 部署。
  • 您必须使用 AmazonLinux2 Docker 镜像。
  • 您必须设置以下 IronPdfEngineWorkingDirectory:
import com.ironsoftware.ironpdf.Settings;
import java.nio.file.Paths;

// Setting the working directory for IronPDF engine
Settings.setIronPdfEngineWorkingDirectory(Paths.get("/tmp/"));
import com.ironsoftware.ironpdf.Settings;
import java.nio.file.Paths;

// Setting the working directory for IronPDF engine
Settings.setIronPdfEngineWorkingDirectory(Paths.get("/tmp/"));
JAVA

注意:这是必须的,因为这是 AWS 允许的唯一执行环境路径。

  • 增加 /tmp 大小。默认值为 512 MB。 请将其设置为至少 1024 MB。
  • 在您的项目中包含 ironpdf-engine-linux-x64 依赖关系:
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-linux-x64</artifactId>
    <version>2022.xx.x</version>
</dependency>
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-linux-x64</artifactId>
    <version>2022.xx.x</version>
</dependency>
XML
  • 由于启动缓慢,将 Lambda 超时设置为 330 秒。
  • 将 Lambda 内存大小至少设置为 1024 MB。

使用AWS Toolkit for IntelliJ IDEA (AWS SAM) 的快速入门

1.安装工具:

2.创建项目: (<代码>文件</代码> -> <代码>新建</代码> -> <代码>项目...</代码)

AWS Lambda 项目创建

  1. 配置:
    • 包类型:Image
    • 运行时:<代码>java8</代码>或<代码>java11</代码
    • SAM 模板:<代码>Maven</代码

!a href="/static-assets/IronPDF-java/howto/aws2.webp">AWS Lambda 配置。

  1. 将以下依赖项添加到你的 pom.xml
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>2.0.3</version>
</dependency>
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-linux-x64</artifactId>
    <version>2022.11.1</version>
</dependency>
<dependency>
    <groupId>io.perfmark</groupId>
    <artifactId>perfmark-api</artifactId>
    <version>0.26.0</version>
</dependency>
<dependency>
    <groupId>io.grpc</groupId>
    <artifactId>grpc-okhttp</artifactId>
    <version>1.50.2</version>
</dependency>
<dependency>
    <groupId>io.grpc</groupId>
    <artifactId>grpc-netty-shaded</artifactId>
    <version>1.50.2</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>2.0.3</version>
</dependency>
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-linux-x64</artifactId>
    <version>2022.11.1</version>
</dependency>
<dependency>
    <groupId>io.perfmark</groupId>
    <artifactId>perfmark-api</artifactId>
    <version>0.26.0</version>
</dependency>
<dependency>
    <groupId>io.grpc</groupId>
    <artifactId>grpc-okhttp</artifactId>
    <version>1.50.2</version>
</dependency>
<dependency>
    <groupId>io.grpc</groupId>
    <artifactId>grpc-netty-shaded</artifactId>
    <version>1.50.2</version>
</dependency>
XML
  1. handleRequest 函数代码更改为 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;

// AWS Lambda function to generate a PDF from a URL using IronPDF.
public class App {
    public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) {
        APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();

        // Enable debugging for IronPDF (optional)
        Settings.setDebug(true);

        // Set the working directory for the IronPDF engine (required)
        Settings.setIronPdfEngineWorkingDirectory(Paths.get("/tmp/"));

        try {
            context.getLogger().log("RENDER PDF");

            // Render the PDF from a URL
            PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://www.google.com");

            context.getLogger().log("RENDER PDF SUCCESS");

            // Save the generated PDF to a file
            pdf.saveAs("/tmp/my-first-pdf.pdf");

            // Set HTTP response headers
            Map<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "application/json");
            headers.put("X-Custom-Header", "application/json");

            // Return the successful response
            return response
                    .withHeaders(headers)
                    .withStatusCode(200)
                    .withBody("ENJOY IRON-PDF!");

        } catch (Exception e) {
            // Return the error response
            return response
                    .withBody("{" + e.getMessage() + "}")
                    .withStatusCode(500);
        }
    }
}
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;

// AWS Lambda function to generate a PDF from a URL using IronPDF.
public class App {
    public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) {
        APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();

        // Enable debugging for IronPDF (optional)
        Settings.setDebug(true);

        // Set the working directory for the IronPDF engine (required)
        Settings.setIronPdfEngineWorkingDirectory(Paths.get("/tmp/"));

        try {
            context.getLogger().log("RENDER PDF");

            // Render the PDF from a URL
            PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://www.google.com");

            context.getLogger().log("RENDER PDF SUCCESS");

            // Save the generated PDF to a file
            pdf.saveAs("/tmp/my-first-pdf.pdf");

            // Set HTTP response headers
            Map<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "application/json");
            headers.put("X-Custom-Header", "application/json");

            // Return the successful response
            return response
                    .withHeaders(headers)
                    .withStatusCode(200)
                    .withBody("ENJOY IRON-PDF!");

        } catch (Exception e) {
            // Return the error response
            return response
                    .withBody("{" + e.getMessage() + "}")
                    .withStatusCode(500);
        }
    }
}
JAVA
  1. template.yaml 中设置 Lambda 配置:
Globals:
  Function:
    Timeout: 400
    MemorySize: 2048
    EphemeralStorage:
      Size: 1024
# Do not modify other configurations
Globals:
  Function:
    Timeout: 400
    MemorySize: 2048
    EphemeralStorage:
      Size: 1024
# Do not modify other configurations
YAML

7.更新 Dockerfile:

  • 注意:对于 Java 8,使用 java8.al2 镜像,因为它们使用 AmazonLinux2,而 java8 使用旧的 AmazonLinux
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

FROM public.ecr.aws/lambda/java:8.al2
RUN yum update -y
RUN 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
RUN amazon-linux-extras install epel -y
RUN yum install -y libgdiplus
RUN chmod 777 /tmp/
COPY --from=build-image /task/target/classes /var/task/
COPY --from=build-image /task/target/dependency /var/task/lib

# Command can be overridden by providing a different command in the template directly.
CMD ["helloworld.App::handleRequest"]
  1. 构建项目:
sam build -u
sam build -u
SHELL

9.部署项目:

sam deploy --guided
sam deploy --guided
SHELL

10.在 AWS Lambda 中享受 IronPDF!现在您的功能已在以下网站上线: 访问 AWS Lambda 控制台

常见问题解答

如何在 AWS Lambda 上以 Java 创建和编辑 PDF?

您可以在 AWS Lambda 中使用 IronPDF for Java,通过设置环境,包括使用 'AmazonLinux2' Docker 镜像,并配置必要的设置,如增加 /tmp 目录大小和设置 Lambda 超时。

为什么我无法在 AWS Lambda 中使用 Zip 部署进行 PDF 处理?

因为 Zip 部署不支持在 AWS Lambda 中的 IronPDF 运行,因为它在运行时需要二进制执行。'PackageType' 必须设置为 'Image' 以进行 Docker 部署。

在 AWS Lambda 中 IronPDF 工作目录的必要配置是什么?

将 IronPDF 引擎的工作目录设置为 '/tmp/' 并确保目录大小增加到至少 1024 MB,以有效处理 PDF。

在 AWS Lambda 的 Maven 项目中用于 PDF 生成的依赖项是什么?

在您的 Maven 项目中的 pom.xml 中包括 'ironpdf-engine-linux-x64' 依赖项,以及其他必要的库,以启用 PDF 创建和编辑。

如何在 AWS Lambda 上通过 Java 从 URL 渲染 PDF?

利用 IronPDF 方法 PdfDocument.renderUrlAsPdf 将 URL 转换为 PDF 文档,并使用 pdf.saveAs 方法保存。

PDF 处理的推荐 Lambda 超时和内存设置是什么?

将 Lambda 超时设置为 330 秒,并分配至少 1024 MB 的内存,以满足 IronPDF 在 AWS Lambda 中的处理需求。

在 AWS Lambda 上以 Java 运行 PDF 操作可以使用哪些运行时环境?

IronPDF 支持在 AWS Lambda 上执行 PDF 处理任务的 'java8' 和 'java11' 运行时环境。

如何快速开始在 IntelliJ IDEA 上使用 AWS Toolkit 进行 Java 中的 PDF 处理?

安装 IntelliJ IDEA、AWS Toolkit、SAM CLI 和 Docker。可选地,为了本地测试,设置 Java 8 和 Maven,然后遵循快速入门指南创建并配置您的项目。

使用 SAM CLI 在 AWS Lambda 上部署 Java PDF 处理项目的过程是什么?

首先使用命令 sam build -u 构建您的项目,然后使用 sam deploy --guided 部署它,以有效地设置在 AWS Lambda 中的 PDF 处理。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
版本: 2025.12 刚刚发布