在 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 软件包。

注意:为了便于调试,您可以设置以下内容:

IronPdf.Logging.Logger.EnableDebugging = true;
IronPdf.Logging.Logger.LogFilePath = "Default.log";
IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
IronPdf.Logging.Logger.EnableDebugging = true;
IronPdf.Logging.Logger.LogFilePath = "Default.log";
IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
IronPdf.Logging.Logger.EnableDebugging = True
IronPdf.Logging.Logger.LogFilePath = "Default.log"
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.

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'

3. 结论

总结一下,为了减少初始容器大小:

  • 使用 IronPdf.Slim NuGet 包
  • 配置 IronPdf
Installation.LinuxAndDockerDependenciesAutoConfig = true;
    Installation.AutomaticallyDownloadNativeBinaries = true;
  • 确保应用程序目录可写/可执行

    RUN chmod +rwx /app/

    利用上述第1点和第2点的非常精简的Docker镜像应该将大小从约500MB减少到约200MB。

    如果可能的话,如果您不选择精简部署,您将看到最佳性能。

    一些容器化和云环境是不持久的,因此可能需要偶尔重新下载依赖项,或再次运行apt-get命令,这可能需要几分钟!

    然而,我们理解一些开发者可能面临严格的部署大小要求。