IronPDF を使って AWS Lambda で PDF ファイルを作成する
1.コンテナテンプレートでAWS Lambdaを作成する(.NET 5)
AWSのこの公式文書の最初の部分を参照してください。コンテナイメージを使用した .NET 5 AWS Lambda サポート.
2.パッケージの依存関係を追加する
これらの依存関係は、このAWS環境でChromeのために必要です。
以下の指示に従って、Dockerファイルを変更してください。
AWS Lambda with .NET 5
AWS Lambda と .NET 5
<script src="https://gist.github.com/ironsoftwarebuild/7f2265f7751240398fb532bd318fc90c.js"></script>
<script src="https://gist.github.com/ironsoftwarebuild/7f2265f7751240398fb532bd318fc90c.js"></script>
AWS Lambdaと.NET 7
<script src="https://gist.github.com/ironsoftwarebuild/ea399e109586f3ac29ebd43d1d0f6285.js"></script>
<script src="https://gist.github.com/ironsoftwarebuild/ea399e109586f3ac29ebd43d1d0f6285.js"></script>
AWS Lambdaと.NET 8
<script src="https://gist.github.com/ironsoftwarebuild/b700ca3ee47f405c257e72b2f8a33d52.js"></script>
<script src="https://gist.github.com/ironsoftwarebuild/b700ca3ee47f405c257e72b2f8a33d52.js"></script>
3.IronPdf (Linux (リナックス)) NuGetパッケージの追加
IronPdf.Linux
をインストールする
-
ソリューション エクスプローラーで「リファレンス」を右クリックし、「NuGet パッケージの管理」を選択
-
「参照」を選択して「IronPdf.Linux」を検索
- パッケージを選択してインストールしてください。
4.FunctionHandlerコードの修正
この例では、ウェブページからPDFファイルを作成します。IronPDFのウェブサイトを作成し、/tmp
に保存してください。 このPDFを表示するには、S3などの他のサービスにアップロードする必要があります。
IronPDF on AWS Lambdaを使用する際は、テンポラリフォルダを設定する必要があります。 TempFolderPathとCustomDeploymentDirectory**プロパティを使用してください。
public Casing FunctionHandler(string input, ILambdaContext context)
{
try
{
context.Logger.LogLine($"START FunctionHandler RequestId: {context.AwsRequestId} Input: {input}");
var awsTmpPath = @"/tmp/"; // AWS temporary storage
//[optional] enable logging (please uncomment these code if you face any problem)
//IronPdf.Logging.Logger.EnableDebugging = true;
//IronPdf.Logging.Logger.LogFilePath = awsTmpPath;
//IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
//set your license key
IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY";
//set ChromeGpuMode to Disabled
IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;
//set IronPDF Temp Path
IronPdf.Installation.TempFolderPath = awsTmpPath;
IronPdf.Installation.CustomDeploymentDirectory = awsTmpPath;
//set auto LinuxAndDockerDependenciesAutoConfig
IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig = true;
context.Logger.LogLine($"creating IronPdf.ChromePdfRenderer");
var Renderer = new IronPdf.ChromePdfRenderer();
context.Logger.LogLine($"rendering Pdf");
using var pdfDoc = Renderer.RenderUrlAsPdf("https://ironpdf.com/");
var guid = Guid.NewGuid();
var fileName = $"/tmp/{input}_{guid}.pdf"; //save file to /tmp
context.Logger.LogLine($"saving pdf name : {fileName}");
pdfDoc.SaveAs(fileName);
//you can upload the saved PDF file to anywhere such as S3.
context.Logger.LogLine($"COMPLETE!");
}
catch (Exception e)
{
context.Logger.LogLine($"[ERROR] FunctionHandler : {e.Message}");
}
return new Casing(input?.ToLower(), input?.ToUpper());
}
public Casing FunctionHandler(string input, ILambdaContext context)
{
try
{
context.Logger.LogLine($"START FunctionHandler RequestId: {context.AwsRequestId} Input: {input}");
var awsTmpPath = @"/tmp/"; // AWS temporary storage
//[optional] enable logging (please uncomment these code if you face any problem)
//IronPdf.Logging.Logger.EnableDebugging = true;
//IronPdf.Logging.Logger.LogFilePath = awsTmpPath;
//IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
//set your license key
IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY";
//set ChromeGpuMode to Disabled
IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;
//set IronPDF Temp Path
IronPdf.Installation.TempFolderPath = awsTmpPath;
IronPdf.Installation.CustomDeploymentDirectory = awsTmpPath;
//set auto LinuxAndDockerDependenciesAutoConfig
IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig = true;
context.Logger.LogLine($"creating IronPdf.ChromePdfRenderer");
var Renderer = new IronPdf.ChromePdfRenderer();
context.Logger.LogLine($"rendering Pdf");
using var pdfDoc = Renderer.RenderUrlAsPdf("https://ironpdf.com/");
var guid = Guid.NewGuid();
var fileName = $"/tmp/{input}_{guid}.pdf"; //save file to /tmp
context.Logger.LogLine($"saving pdf name : {fileName}");
pdfDoc.SaveAs(fileName);
//you can upload the saved PDF file to anywhere such as S3.
context.Logger.LogLine($"COMPLETE!");
}
catch (Exception e)
{
context.Logger.LogLine($"[ERROR] FunctionHandler : {e.Message}");
}
return new Casing(input?.ToLower(), input?.ToUpper());
}
Public Function FunctionHandler(ByVal input As String, ByVal context As ILambdaContext) As Casing
Try
context.Logger.LogLine($"START FunctionHandler RequestId: {context.AwsRequestId} Input: {input}")
Dim awsTmpPath = "/tmp/" ' AWS temporary storage
'[optional] enable logging (please uncomment these code if you face any problem)
'IronPdf.Logging.Logger.EnableDebugging = true;
'IronPdf.Logging.Logger.LogFilePath = awsTmpPath;
'IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
'set your license key
IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY"
'set ChromeGpuMode to Disabled
IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled
'set IronPDF Temp Path
IronPdf.Installation.TempFolderPath = awsTmpPath
IronPdf.Installation.CustomDeploymentDirectory = awsTmpPath
'set auto LinuxAndDockerDependenciesAutoConfig
IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig = True
context.Logger.LogLine($"creating IronPdf.ChromePdfRenderer")
Dim Renderer = New IronPdf.ChromePdfRenderer()
context.Logger.LogLine($"rendering Pdf")
Dim pdfDoc = Renderer.RenderUrlAsPdf("https://ironpdf.com/")
Dim guid As System.Guid = System.Guid.NewGuid()
Dim fileName = $"/tmp/{input}_{guid}.pdf" 'save file to /tmp
context.Logger.LogLine($"saving pdf name : {fileName}")
pdfDoc.SaveAs(fileName)
'you can upload the saved PDF file to anywhere such as S3.
context.Logger.LogLine($"COMPLETE!")
Catch e As Exception
context.Logger.LogLine($"[ERROR] FunctionHandler : {e.Message}")
End Try
Return New Casing(input?.ToLower(), input?.ToUpper())
End Function
メモリとタイムアウトの増加
IronPDFはLambdaのデフォルト値よりも多くの時間とメモリを必要とします。 aws-lambda-tools-defaults.json`で設定できます。 以下の内容をお使いの関数に合わせて調整してください。 この例では、512に設定します。(MB)および330(秒).
"function-memory-size" : 512,
"function-timeout" : 330,
また、Lambdaコンソールを使用してこの設定を更新することもできます。 以下に移動しますAWS Lambdaメモリ構成ガイド詳細については、記事をご覧ください。
6. 公開
指示に従ってくださいコンテナイメージを使用した .NET 5 AWS Lambda サポートLambda関数を公開およびテストするためのドキュメント。
7.ぜひお試しください!
以下を使用してLambda関数を有効化できますAWS Lambda コンソールまたは Visual Studio を使用してVisual Studio 用 AWS Toolkit.