如何在 AWS Lambda 上執行與部署 IronPDF for .NET

This article was translated from English: Does it need improvement?
Translated
View the article in English
Amazon Web Services V1 related to 如何在 AWS Lambda 上執行與部署 IronPDF for .NET

1. 使用容器範本建立 AWS Lambda(.NET 5)

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

2. 新增套件依賴項

在這個 AWS 環境中,Chrome 需要這些依賴項。

請根據以下說明修改 Docker 檔案:

AWS Lambda 與 .NET 5

AWS Lambda 與 .NET 7

AWS Lambda 與 .NET 8

3. 新增 IronPDF (Linux) NuGet 套件

安裝 IronPdf.Linux

  1. 在"解決方案總覽"中,右鍵點擊"參考項目",選擇"管理 NuGet 套件"。
  2. 選擇"瀏覽"並搜尋 IronPdf.Linux
  3. 選擇套件並進行安裝。

4. 修改 FunctionHandler 程式碼

此範例將從網頁https://ironpdf.com/建立 PDF 檔案,並儲存至 /tmp。 若要檢視此 PDF 檔案,您必須將其上傳至其他服務(例如 S3)。

在 AWS Lambda 上使用 IronPDF 時,必須設定暫存資料夾。 請使用 TempFolderPathCustomDeploymentDirectory 屬性來執行此操作。

public Casing FunctionHandler(string input, ILambdaContext context)
{
    try
    {
        context.Logger.LogLine($"START FunctionHandler RequestId: {context.AwsRequestId} Input: {input}");

        var awsTmpPath = @"/tmp/"; // AWS temporary storage location

        // [Optional] Enable logging for debugging
        // Uncomment these lines if you encounter issues
        // IronPdf.Logging.Logger.EnableDebugging = true;
        // IronPdf.Logging.Logger.LogFilePath = awsTmpPath;
        // IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;

        // Set your IronPDF license key
        IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY";

        // Disable GPU for Chrome rendering in headless environments
        IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;

        // Configure IronPDF to use the AWS temporary directory
        IronPdf.Installation.TempFolderPath = awsTmpPath;
        IronPdf.Installation.CustomDeploymentDirectory = awsTmpPath;

        // Automatically configure Linux and Docker dependencies
        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 with name: {fileName}");
        pdfDoc.SaveAs(fileName);

        // Place for future code to upload the PDF file to a service like AWS 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 location

        // [Optional] Enable logging for debugging
        // Uncomment these lines if you encounter issues
        // IronPdf.Logging.Logger.EnableDebugging = true;
        // IronPdf.Logging.Logger.LogFilePath = awsTmpPath;
        // IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;

        // Set your IronPDF license key
        IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY";

        // Disable GPU for Chrome rendering in headless environments
        IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;

        // Configure IronPDF to use the AWS temporary directory
        IronPdf.Installation.TempFolderPath = awsTmpPath;
        IronPdf.Installation.CustomDeploymentDirectory = awsTmpPath;

        // Automatically configure Linux and Docker dependencies
        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 with name: {fileName}");
        pdfDoc.SaveAs(fileName);

        // Place for future code to upload the PDF file to a service like AWS 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 location

		' [Optional] Enable logging for debugging
		' Uncomment these lines if you encounter issues
		' IronPdf.Logging.Logger.EnableDebugging = true;
		' IronPdf.Logging.Logger.LogFilePath = awsTmpPath;
		' IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;

		' Set your IronPDF license key
		IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY"

		' Disable GPU for Chrome rendering in headless environments
		IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled

		' Configure IronPDF to use the AWS temporary directory
		IronPdf.Installation.TempFolderPath = awsTmpPath
		IronPdf.Installation.CustomDeploymentDirectory = awsTmpPath

		' Automatically configure Linux and Docker dependencies
		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 with name: {fileName}")
		pdfDoc.SaveAs(fileName)

		' Place for future code to upload the PDF file to a service like AWS 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. 增加記憶體與超時設定

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 控制台,或使用 Visual Studio 的 AWS Toolkit for Visual Studio 來啟用 Lambda 函式。

常見問題

如何使用.NET AWS Lambda功能從URL建立PDF?

要在.NET AWS Lambda功能中從URL建立PDF,可使用IronPDF的ChromePdfRenderer.RenderUrlAsPdf方法。確保臨時文件夾通過TempFolderPathCustomDeploymentDirectory屬性配置。

我應該採取哪些步驟來配置IronPDF在AWS Lambda上?

通過設置臨時文件夾路徑TempFolderPathCustomDeploymentDirectory,禁用GPU使用ChromeGpuModes.Disabled,並將LinuxAndDockerDependenciesAutoConfig設為true啟用Linux和Docker依賴項的自動配置來配置IronPDF。

為什麼調整AWS Lambda的記憶體和超時設定很重要?

調整AWS Lambda的記憶體和超時設定至關重要,因為IronPDF比預設設定需要更多資源。這可以在aws-lambda-tools-defaults.json或AWS Lambda主控台中配置。

如何確保Chrome依賴項正確安裝於AWS Lambda?

確保Chrome依賴項正確安裝,方法是根據文章中提供的指示按您的.NET版本修改Dockerfile。這包括在AWS環境安裝Chrome所需的Linux套件。

我可以使用Visual Studio激活並測試我的AWS Lambda功能嗎?

可以,您可以使用Visual Studio的AWS工具包激活並測試您的AWS Lambda功能,這允許在IDE中直接進行無縫部署和測試。

在AWS Lambda上使用.NET 5的容器範本有何意義?

在AWS Lambda上使用.NET 5的容器範本很重要,因為它有助於應用程式及其依賴項的部署,如IronPDF,確保跨環境一致性。

如何使用IronPDF在AWS Lambda功能中處理例外?

通過將您的IronPDF操作包裹在try-catch塊中處理AWS Lambda功能中的例外,並使用context.Logger.LogLine記錄錯誤以進行除錯和故障排除。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備開始了嗎?
Nuget 下載 20,296,129 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想快速獲得證明嗎? PM > Install-Package IronPdf
執行範例 看您的HTML變成PDF。