如何在 AWS Lambda 中運行 IronPDF for Java
重要事項:必要設置
- Zip 部署不支持,因為 IronPDF 需要在運行時執行二進制文件。
- 您必須將
PackageType
設置為Image
。因為 IronPDF for Java 只支持 Docker 部署。 - 您必須使用
AmazonLinux2
docker 映像。 - 您必須設置以下 IronPdfEngineWorkingDirectory:
Setting.setIronPdfEngineWorkingDirectory(Paths.get("/tmp/"));
必要 因為這是 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>
- 將 Lambda 超時時間設置為 330 秒,以應對啓動速度緩慢的情況。
- 將 Lambda 記憶體大小設置為至少 1024 MB。
使用 AWS Toolkit for IntelliJ IDEA 快速入門 (AWS SAM)
安裝工具:
IntelliJ IDEA - https://www.jetbrains.com/idea/download/
AWS 工具包 - https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/setup-toolkit.html
SAM CLI - SAM 命令行界面 安裝 SAM CLI
- Docker - Docker 安裝 Docker 社區版
您可能還需要以下內容用於本地測試:
Java8 安裝 Java SE 開發工具包 8
* Maven - [安裝 Maven](https://maven.apache.org/install.html)
- 建立專案。 (
文件
->新建
->專案...
)
配置:
包类型 :
Image
运行时 :
java8
或java11
- SAM 模板 :
Maven
- 在您的
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>
- 將
App.java
中的handleRequest
函數代碼更改為:
import com.ironsoftware.ironpdf.*;
public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) {
APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();
Settings.setDebug(true); //optional
Settings.setIronPdfEngineWorkingDirectory(Paths.get("/tmp/")); //requried!
try {
context.getLogger().log("RENDER PDF");
PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://www.google.com");
context.getLogger().log("RENDER PDF SUCCESS");
pdf.saveAs("/tmp/my-first-pdf.pdf");
//Done! Now you can do anything with the pdf such as upload this pdf to S3.
//return something..
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("X-Custom-Header", "application/json");
APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent()
.withHeaders(headers);
return response
.withStatusCode(200)
.withBody("ENJOY IRON-PDF!");
} catch (Exception e) {
return response
.withBody("{" + e.getMessage() + "}")
.withStatusCode(500);
}
}
- 在
template.yaml
中設定 Lambda 配置:
Globals:
Function:
Timeout: 400
MemorySize: 2048
EphemeralStorage:
Size: 1024
#don't touch the other config
- 將 Dockerfile 更新為以下內容:
- 注意:對於 Java8,請使用
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
RUN yum install -y libXcomposite.x86_64
RUN yum install -y libXcursor.x86_64
RUN yum install -y libXdamage.x86_64
RUN yum install -y libXext.x86_64
RUN yum install -y libXi.x86_64
RUN yum install -y libXtst.x86_64
RUN yum install -y cups-libs.x86_64
RUN yum install -y libXScrnSaver.x86_64
RUN yum install -y libXrandr.x86_64
RUN yum install -y GConf2.x86_64
RUN yum install -y alsa-lib.x86_64
RUN yum install -y atk.x86_64
RUN yum install -y gtk3.x86_64
RUN yum install -y ipa-gothic-fonts
RUN yum install -y xorg-x11-fonts-100dpi
RUN yum install -y xorg-x11-fonts-75dpi
RUN yum install -y xorg-x11-utils
RUN yum install -y xorg-x11-fonts-cyrillic
RUN yum install -y xorg-x11-fonts-Type1
RUN yum install -y xorg-x11-fonts-misc
RUN yum install -y glibc-devel.x86_64
RUN yum install -y at-spi2-atk.x86_64
RUN yum install -y mesa-libgbm.x86_64
RUN yum install -y libxkbcommon
RUN yum install -y 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 overwritten by providing a different command in the template directly.
CMD ["helloworld.App::handleRequest"]
- 使用以下方式建構:
sam build -u
- 部署使用:
sam deploy --guided
- 在 AWS Lambda 中享受 IronPDF! 現在您的功能已啟動於: https://console.aws.amazon.com/lambda/home