AWS LambdaでIronPDF for Javaを実行する方法
重要: 必要な設定
- IronPDFは実行時にバイナリの実行を必要とするため、ZIPデプロイはサポートされていません。
PackageType
をImage
に設定する必要があります。 IronPDF for JavaはDockerデプロイメントのみをサポートしているためです。AmazonLinux2
ドッカーイメージを使用する必要があります。- 次のIronPdfEngineWorkingDirectoryを設定する必要があります:
Setting.setIronPdfEngineWorkingDirectory(Paths.get("/tmp/"));
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>
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf-engine-linux-x64</artifactId>
<version>2022.xx.x</version>
</dependency>
- 起動が遅いため、Lambdaのタイムアウトを330秒に設定します。
- Lambdaのメモリサイズを少なくとも1024MBに設定してください。
IntelliJ IDEA用AWSツールキット(AWS SAM)のクイックスタート
-
ツールのインストール:
-
IntelliJ IDEA - IntelliJ IDEAをダウンロード
-
AWS Toolkit - JetBrains 用 AWS Toolkit のセットアップ
-
SAM CLI - サーバーレスアプリケーション用のSAM CLIをインストールする
- Docker - Docker Community Editionをインストール
ローカルテストのために次のものが必要な場合もあります:
- Maven - Mavenインストールガイドライン
-
-
プロジェクトを作成。 (
ファイル
->新規
->プロジェクト...
) -
設定:
-
パッケージタイプ:
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>
<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);
}
}
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
Globals:
Function:
Timeout: 400
MemorySize: 2048
EphemeralStorage:
Size: 1024
#don't touch the other config
- 以下のようにDockerfileを更新してください:
- 注:Java8を使用する場合は、
AmazonLinux2
を使用しているためjava8.al2
イメージを使用してください。ただし、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 build -u
- 次の方法でデプロイ:
sam deploy --guided
sam deploy --guided
- AWS LambdaでIronPDFをお楽しみください! これで、関数は次の場所で利用可能です: AWS Lambda コンソールにアクセス