Running IronPDF on Linux: Setup Guide

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 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 16,154,058 | 版本: 2025.11 刚刚发布