在Linux上運行IronPDF:設置指南
IronPDF for 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
如何製作更小的Docker影像
1. 在運行時安裝套件
減少大小的一種方法是在運行時而不是在構建Docker影像時運行apt-get命令:
- 從您的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/)。
注意:第一次初始化會較慢,因為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命令,這可能需要幾分鐘!
然而,我們理解某些開發者可能會受到部署大小限制的嚴格要求。

