Verwenden von IronPDF zum Erstellen von PDF-Dateien auf AWS Lambda

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

1. AWS Lambda mit einer Container-Vorlage erstellen (.NET 5)

Bitte beziehen Sie sich auf den ersten Teil dieses offiziellen Dokuments von AWS: .NET 5 AWS Lambda-Unterstützung mit Container-Abbildern.

2. Paketabhängigkeiten hinzufügen

Diese Abhängigkeiten sind für Chrome in dieser AWS-Umgebung erforderlich.

Bitte ändern Sie die Docker-Datei entsprechend dieser Anleitung:

AWS Lambda mit .NET 5

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 Packages2. Select Browse and search IronPdf.Linux3. Select the package and install.

    4. Modify FunctionHandler code

    This example will create a pdf file from a webpage https://ironpdf.com/ and save to /tmp. In order to view this pdf you must upload this pdf 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. csharppublic 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); //here you can upload 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());}

    5. Increase Memory and Timeout

    IronPDF requires more time and memory than the default value of Lambda. You can configure it at 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) csharp"function-memory-size" : 512, "function-timeout" : 330, You also can update this configuration using the Lambda console. Navigate to the Configuring AWS Lambda functions 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 Lambda console or via Visual Studio by using the AWS Toolkit for Visual Studio.