在 AWS Lambda 上使用 IronPDF 創建 PDF 文件
1. 使用容器模板創建 AWS Lambda (.NET 5)
請參閱AWS官方文件的第一部分:.NET 5 AWS Lambda 支援容器映像文件.
2. 添加封裝相依性
這些依賴項是在此AWS環境中運行Chrome所必需的。
請根據以下說明修改 Docker 文件:
使用 .NET 5 的 AWS Lambda
<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 於 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
5. 增加記憶體和超時时间
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 工具包.