在 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 套件,才能讓 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
$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 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
Nuget 下載 19,014,616 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronPdf
執行範例 觀看您的 HTML 轉為 PDF。