在 Linux 上執行 IronPDF:設定指南
IronPDF for Linux 約 280MB(包含 IronPDF 程式碼及完整的 Chrome 瀏覽器)。 Docker 映像檔大小約為 500MB。
Chrome 還需要一些基本套件,具體取決於您的 Linux 發行版。 大多數 Linux 發行版已預先安裝這些套件,因為它們被 Linux 上的各種應用程式和函式庫所使用。
然而,若您使用的是精簡版發行版,則需安裝必要的 Linux 套件,才能讓 Chrome 在 Linux 上運行。
注意:為便於除錯,您可以設定以下項目:
// 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
如何製作更小的 Docker 映像檔
1. 執行時安裝套件
減少檔案大小的其中一種方法,是在執行時執行 apt-get 指令,而非在建立 Docker 映像檔時執行:
- 請從您的 Dockerfile 中移除
apt-get指令。 - 在初始化 IronPDF 或渲染文件之前,請設定
Installation.LinuxAndDockerDependenciesAutoConfig = true;。 - 請確保您的應用程式具備足夠的權限,以執行
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.
2. 使用 IronPdf.Slim
您亦可選擇 僅 包含 IronPdf.dll,並在執行時下載所需的檔案。
- 從您的專案中移除 IronPdf.Linux(以及任何其他 IronPDF)NuGet 套件,並新增 IronPdf.Slim。
- 在初始化 IronPDF 或渲染文件之前,請設定
Installation.AutomaticallyDownloadNativeBinaries = true;。 - 請確保您的 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'
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
- 確保應用程式目錄具有寫入權限/執行權限:
RUN chmod +rwx /app/
若採用同時運用第 1 點與第 2 點的極精簡 Docker 映像檔,應可將大小從約 500MB 減至約 200MB。
若情況允許,建議您不要選擇精簡部署模式,如此將能獲得最佳效能。
某些容器化與雲端環境具有 非持久性 的特性,因此可能需要不時重新下載依賴項,或再次執行 apt-get 指令,此過程可能耗時長達數分鐘!
然而,我們理解部分開發人員在部署時可能面臨嚴格的檔案大小限制。

