在 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命令,这可能需要几分钟时间!
然而,我们理解某些开发人员可能面临其部署的严格大小要求。

