在 Linux 上運行 IronPDF:設定指南

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

IronPdf Linux 版約 280MB(包括 IronPdf 程式碼和整個 Chrome 瀏覽器)。 Docker 映像大小約為 500MB。

Chrome 也需要一些基本軟體包,這些軟體包會根據您的 Linux 發行版而有所不同。 大多數 Linux 發行版都已安裝了這些軟體包,因為 Linux 上的各種應用程式和函式庫都會使用它們。

但是,如果您使用的是精簡版發行版,則需要安裝必要的 Linux 軟體套件才能在 Linux 上執行 Chrome。

注意:為了方便調試,您可以設定以下參數:

// Enable detailed debugging for IronPdf
IronPdf.Logging.Logger.EnableDebugging = true;

// Specify the log file path
IronPdf.Logging.Logger.LogFilePath = "Default.log";

// Set the logging mode to capture all log data
IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
// Enable detailed debugging for IronPdf
IronPdf.Logging.Logger.EnableDebugging = true;

// Specify the log file path
IronPdf.Logging.Logger.LogFilePath = "Default.log";

// Set the logging mode to capture all log data
IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
' Enable detailed debugging for IronPdf
IronPdf.Logging.Logger.EnableDebugging = True

' Specify the log file path
IronPdf.Logging.Logger.LogFilePath = "Default.log"

' Set the logging mode to capture all log data
IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All
$vbLabelText   $csharpLabel

如何建立更小的 Docker 映像

1. 運行時安裝軟體包

減小映像大小的一種方法是,在運行時運行apt-get命令,而不是在建置 Docker 映像時執行:

  1. 從 Dockerfile 中刪除apt-get指令。
  2. 在初始化 IronPdf 或渲染文件之前,設定Installation.LinuxAndDockerDependenciesAutoConfig = true;
  3. 確保您的應用程式具有運行apt-get命令所需的足夠權限。

注意:首次初始化速度會較慢,因為apt-get命令必須在渲染第一個文件之前完成,並且每次重新部署鏡像時都會重複此程序。

注意:您應該會在控制台/日誌中看到指示軟體包安裝成功的條目:

Executing command 'apt install -y libnss3' in '/app/bin/Debug/netcoreapp3.1/runtimes/linux-x64/native'
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
  libnspr4
The following NEW packages will be installed:
  libnspr4 libnss3
0 upgraded, 2 newly installed, 0 to remove and 9 not upgraded.
Executing command 'apt install -y libnss3' in '/app/bin/Debug/netcoreapp3.1/runtimes/linux-x64/native'
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
  libnspr4
The following NEW packages will be installed:
  libnspr4 libnss3
0 upgraded, 2 newly installed, 0 to remove and 9 not upgraded.
SHELL

2. 使用 IronPdf.Slim

您也可以選擇_僅_包含 IronPdf.dll,並在執行時下載必要的檔案。

  1. 從你的專案中移除 IronPdf.Linux(以及任何其他 IronPdf)NuGet 包,並加入 IronPdf.Slim。
  2. 在初始化 IronPdf 或渲染文件之前,設定Installation.AutomaticallyDownloadNativeBinaries = true;
  3. 確保您的 Dockerfile 授予對應用程式整個工作目錄的讀取和寫入權限(例如,將RUN chmod +rwx /app/runtimes/linux-x64/native/IronCefSubprocess更改為RUN chmod +rwx /app/ )。

注意:首次初始化速度會較慢,因為 NuGet 套件需要下載並解壓縮才能渲染第一個文檔,每次重新部署映像時都會重複此過程。

注意:您應該會在控制台/日誌中看到指示下載和解壓成功的條目:

Downloading NuGet package from 'https://www.nuget.org/api/v2/package/IronPdf.Native.Chrome.Linux/2023.1.11387'
Extracting package contents '/app/bin/Debug/netcoreapp3.1/IronPdf.Native.Chrome.Linux.2023.1.11387.nupkg/runtimes/linux-x64/native' to '/app/bin/Debug/netcoreapp3.1/runtimes/linux-x64/native'
Successfully deployed NuGet package 'IronPdf.Native.Chrome.Linux' to '/app/bin/Debug/netcoreapp3.1/runtimes/linux-x64/native'
Successfully located 'IronInterop' at '/app/bin/Debug/netcoreapp3.1/runtimes/linux-x64/native'
Downloading NuGet package from 'https://www.nuget.org/api/v2/package/IronPdf.Native.Chrome.Linux/2023.1.11387'
Extracting package contents '/app/bin/Debug/netcoreapp3.1/IronPdf.Native.Chrome.Linux.2023.1.11387.nupkg/runtimes/linux-x64/native' to '/app/bin/Debug/netcoreapp3.1/runtimes/linux-x64/native'
Successfully deployed NuGet package 'IronPdf.Native.Chrome.Linux' to '/app/bin/Debug/netcoreapp3.1/runtimes/linux-x64/native'
Successfully located 'IronInterop' at '/app/bin/Debug/netcoreapp3.1/runtimes/linux-x64/native'
SHELL

3. 結論

總之,為了減少初始容器大小:

  • 使用 IronPdf.Slim NuGet 套件。
  • 配置 IronPdf:
// Set installation options for Linux and Docker environments
Installation.LinuxAndDockerDependenciesAutoConfig = true;
Installation.AutomaticallyDownloadNativeBinaries = true;
// Set installation options for Linux and Docker environments
Installation.LinuxAndDockerDependenciesAutoConfig = true;
Installation.AutomaticallyDownloadNativeBinaries = true;
' Set installation options for Linux and Docker environments
Installation.LinuxAndDockerDependenciesAutoConfig = True
Installation.AutomaticallyDownloadNativeBinaries = True
$vbLabelText   $csharpLabel
  • 確保應用程式目錄可寫入/可執行:
RUN chmod +rwx /app/

結合第 1 點和第 2 點,一個非常精簡的 Docker 映像應該可以將大小從約 500MB 減少到約 200MB。

如果可能的話,不選擇精簡部署方式將獲得最佳效能。

某些容器化和雲端環境不持久,因此可能需要偶爾重新下載依賴項或再次執行apt-get命令,這可能需要幾分鐘時間!

但是,我們也了解到,有些開發者在部署時可能會受到嚴格的規模要求。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 16,685,821 | 版本: 2025.12 剛發表