在 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.修改函数处理程序代码
此示例将从网页创建 PDF 文件IronPDF 网站并将其保存到 /tmp
。 要查看此 PDF,您必须将其上传到 S3 等其他服务。
在 AWS Lambda 上使用 IronPDF 时,有必要配置临时文件夹。 请使用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 "后半部分的说明进行翻译。使用容器映像支持 .NET 5 AWS Lambda'文档来发布和测试您的 Lambda 函数。
7.试用!
您可以使用AWS Lambda 控制台或通过 Visual Studio 使用用于 Visual Studio 的 AWS 工具包.