How to Run IronPDF for Java in AWS Lambda

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

重要です:必要な設定

  • Zip展開はIronPDFが実行時にバイナリの実行を必要とするため、サポートされていません。
  • IronPDF for JavaはDockerデプロイメントにのみ対応しているため、PackageTypeImageに設定する必要があります。
  • 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のサイズを大きくしてください。デフォルト値は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>
XML
  • 起動が遅いため、Lambdaのタイムアウトを330秒に設定。
  • Lambdaのメモリサイズを1024MB以上に設定してください。

Quick Start with AWS Toolkit for IntelliJ IDEA (AWS SAM)(英語

1.インストールツール: </strong

2.プロジェクトの作成: (File -> New -> Project...)

AWSラムダプロジェクトの作成

3.コンフィギュレーション:

  • パッケージの種類<コード>イメージ</コード
  • ランタイムjava8またはjava11
  • SAMテンプレート<コード>Maven</コード

AWSラムダ構成

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>
XML

5.App.javahandleRequest関数のコードを次のように変更してください:

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

6.template.yamlにラムダ設定を設定する:

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が古い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 -u
sam build -u
SHELL

9.プロジェクトをデプロイする:

sam deploy --guided
sam deploy --guided
SHELL

10.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処理を効果的に設定するためにデプロイします。

Curtis Chau
テクニカルライター

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。

準備はいいですか?
バージョン: 2025.11 ただ今リリースされました