在 AWS Lambda 上使用 IronPDF 創建 PDF 文件

查克尼思·賓
查克尼思·賓
2023年1月25日
已更新 2024年12月17日
分享:
This article was translated from English: Does it need improvement?
Translated
View the article in English

1. 使用容器模板創建 AWS Lambda (.NET 5)

請參閱AWS官方文件的第一部分:.NET 5 AWS Lambda 支援容器映像文件.

2. 添加封裝相依性

這些依賴項是在此AWS環境中運行Chrome所必需的。

請根據以下說明修改 Docker 文件:

使用 .NET 5 的 AWS Lambda

AWS Lambda with .NET 7

AWS Lambda with .NET 8

3. Add the IronPDF (Linux) NuGet Package

Install IronPdf.Linux

  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search IronPdf.Linux
  3. Select the package and install.

4. Modify FunctionHandler Code

This example will create a PDF file from a webpage IronPDF website and save it to /tmp. In order to view this PDF, you must upload it to another service such as S3.

It is necessary to configure the temporary folder when using IronPDF on AWS Lambda. Please use the TempFolderPath and CustomDeploymentDirectory properties to do this.

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
$vbLabelText   $csharpLabel

5. Increase Memory and Timeout

IronPDF requires more time and memory than the default value of Lambda. You can configure it in aws-lambda-tools-defaults.json. Please adjust this to match your function. In this example, we will set it to 512 (MB) and 330 (seconds).

"function-memory-size" : 512, 
"function-timeout"     : 330,

You also can update this configuration using the Lambda console. Navigate to the AWS Lambda Memory Configuration Guide article for more information.

6. Publish

Please follow the instructions in the latter part of the '.NET 5 AWS Lambda Support with Container Images' document to publish and test your Lambda function.

7. Try it Out!

You can activate the Lambda function using the AWS Lambda Console or via Visual Studio by using the AWS Toolkit for Visual Studio.

查克尼思·賓
軟體工程師
Chaknith 致力於 IronXL 和 IronBarcode。他在 C# 和 .NET 方面擁有豐富的專業知識,協助改進軟體並支持客戶。他從用戶互動中獲得的洞察力有助於提高產品、文檔和整體體驗。