AWSラムダでIronPDF for Javaを実行する方法
重要です:必要な設定
- Zip展開はIronPDFが実行時にバイナリの実行を必要とするため、サポートされていません。
- You must set
PackageTypetoImagebecause IronPDF for Java only supports Docker deployment. 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/"));注意: AWSが実行環境として許可している唯一のパスであるため、これは必須です。
/tmpのサイズを大きくしてください。デフォルト値は512MBです。 最低1024MBに設定してください。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以上に設定してください。
Quick Start with AWS Toolkit for IntelliJ IDEA (AWS SAM)
1.インストールツール:
- IntelliJ IDEA - IntelliJ IDEAをダウンロード。
- AWS Toolkit - JetBrains 用 AWS Toolkit のセットアップ.
- SAM CLI - SAM CLI for Serverless Applications をインストールする。
- Docker - Dockerコミュニティ版をインストールする。
オプション(ローカルテスト用):。
- Java 8 - Java SE Development Kit 8 をダウンロードしてください。
- Maven - Mavenをインストールするためのガイドライン。
2.プロジェクトの作成: (File -> New -> Project...)
- 設定:
- パッケージの種類<コード>イメージ</コード
- ランタイム
java8またはjava11。 - SAMテンプレート<コード>Maven</コード
4.次の依存関係をpom.xmlに追加:
4.次の依存関係を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>5.App.javaのhandleRequest関数のコードを次のように変更してください:。
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);
}
}
}6.template.yamlにラムダ設定を設定する:。
Globals:
Function:
Timeout: 400
MemorySize: 2048
EphemeralStorage:
Size: 1024
# Do not modify other configurationsGlobals:
Function:
Timeout: 400
MemorySize: 2048
EphemeralStorage:
Size: 1024
# Do not modify other configurations7.Dockerfileを更新する:。
- 注:Java 8については、
java8が古いAmazonLinuxを使用しているのに対し、AmazonLinux2を使用しているため、java8.al2画像を使用してください。
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"]8.プロジェクトを構築する:。
sam build -usam build -u9.プロジェクトをデプロイする:。
sam deploy --guidedsam deploy --guided10.AWSラムダでIronPDFを楽しもう!現在、あなたの機能はライブです:AWSラムダコンソールにアクセスしてください。
よくある質問
AWS LambdaでJavaでPDFを作成および編集するにはどうすればよいですか?
AmazonLinux2のDockerイメージを使用して環境を設定し、/tmpディレクトリサイズの増加やLambdaのタイムアウト設定など、必要な設定を行うことで、AWS Lambda内でIronPDF for Javaを使用できます。
AWS LambdaでのPDF処理にZipデプロイメントを使用できないのはなぜですか?
AWS LambdaでIronPDFを使用するには実行時にバイナリ実行が必要なため、Zipデプロイメントはサポートされていません。Dockerデプロイメントでは'PackageType'を'Image'に設定する必要があります。
AWS LambdaにおけるIronPDFの作業ディレクトリの必要な設定は何ですか?
IronPDFエンジンの作業ディレクトリを'/tmp/'に設定し、PDF処理を効率的に行うためにディレクトリサイズを少なくとも1024MBに増やします。
AWS Lambda上のMavenプロジェクトでPDF生成のために含めるべき依存関係は何ですか?
Mavenプロジェクトのpom.xmlに'ironpdf-engine-linux-x64'依存関係を含め、PDFの作成と編集を可能にする他の必要なライブラリを追加します。
Javaを使用してAWS LambdaでURLからPDFをレンダリングするにはどうすればよいですか?
IronPDFのメソッドPdfDocument.renderUrlAsPdfを使用してURLをPDF文書に変換し、pdf.saveAsメソッドで保存します。
PDF処理に推奨されるLambdaのタイムアウトおよびメモリ設定は何ですか?
Lambdaのタイムアウトを330秒に設定し、IronPDFのAWS Lambda内での処理ニーズを満たすためにメモリを少なくとも1024MBに割り当てます。
AWS Lambda上でJavaでPDF操作を実行するためのランタイム環境はどれですか?
IronPDFは、AWS Lambda上でPDF処理タスクを実行するために'java8'および'java11'ランタイム環境をサポートしています。
JavaでPDF処理を行うためのIntelliJ IDEA用AWS Toolkitについてどうすればすぐに開始できますか?
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処理を効果的に設定します。







