在Linux上運行IronPDF:設置指南

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

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
$vbLabelText   $csharpLabel

如何製作更小的Docker影像

1. 在運行時安裝套件

減少大小的一種方法是在運行時而不是在構建Docker影像時運行apt-get命令:

  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/)。

注意:第一次初始化會較慢,因為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擁有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。