跳转到内容

在 CodeIgniter 4 中安装 NextPDF

使用 Composer 安装此包。CodeIgniter 4 会自动发现 Services 类和相关助手函数。你无需手动注册任何内容。

此包的 composer.json 声明了以下约束,该文件才是权威来源。下表仅重述这些约束。

依赖约束备注
PHP>=8.4 <9.0此包面向 PHP 8.4。
nextpdf/core^3.0 || ^5.2NextPDF 引擎。
codeigniter4/framework^4.6已在 CodeIgniter 4.7.0 上验证。
ext-mbstring运行时必需每个进程会验证一次。
ext-zlib运行时必需每个进程会验证一次。

可选包声明在 suggest 下:

新增功能
nextpdf/artisanChrome CDP HTML 渲染器(在文档构建时自动检测)。
nextpdf/premiumNextPDF Pro 与 Enterprise:签名、PDF/A、Factur-X。
codeigniter4/queue通过 GeneratePdfJob 进行异步 PDF 生成。

安装此包:

Terminal window
composer require nextpdf/codeigniter

Composer 会根据上述约束解析 nextpdf/corecodeigniter4/framework。你无需编辑服务提供者、bundle 或引导文件。

Config\Modules::$discoverInComposertrue(即框架默认值)时,CodeIgniter 4 会扫描 Composer 包,查找框架元素。此包随附一个 NextPDF\CodeIgniter\Config\Services 类。该类位于 PSR-4 命名空间 NextPDF\CodeIgniter\ 下,并映射到 src/CodeIgniter/。Composer 的 PSR-4 自动加载器会将该完全限定类名转换为文件路径。顶层命名空间是必需的(PSR-4 §x1.x2.p5,规范性 MUST)。命名空间前缀会映射到基目录,因此该类会解析到对应文件(PSR-4 §x1.x3)。

此包的 Composer files 自动加载项(src/CodeIgniter/Helpers/pdf_helper.php)会注册两个助手函数:pdf()pdf_document()。此包的 Registrar 还会向 CodeIgniter 的助手加载器声明 pdf 助手。完整流程请见 /integrations/codeigniter/boot-and-discovery/。

确认 Composer 已解析到此包:

Terminal window
composer show nextpdf/codeigniter

确认 CodeIgniter 已发现 Services 类。在任意控制器中,或在一个简短的 php spark 路由中,调用一个服务并断言其类型:

<?php
declare(strict_types=1);
use NextPDF\CodeIgniter\Config\Services;
use NextPDF\Core\Document;
$document = Services::pdfDocument(false);
// $document is a fresh NextPDF\Core\Document instance.
\assert($document instanceof Document);

如果 Services::pdfDocument() 返回 Document,说明发现机制运行正常。如果该调用返回 null,说明发现机制未运行。请见 /integrations/codeigniter/troubleshooting/.

  • 如果宿主应用程序将 Config\Modules::$discoverInComposer 设为 false,请将 nextpdf/codeigniter 加入 $composerPackages['only'] 列表。否则发现机制会跳过此包。
  • 过时的 Composer 自动加载器可能导致 Services 类不可见。升级后请运行 composer dump-autoload
  • 此包仅将 codeigniter4/queue 声明为开发依赖。会派发 GeneratePdfJob 的生产环境应用程序必须自行声明 codeigniter4/queue 依赖。

请通过 HTTPS 从 Packagist 安装。请将解析出的版本锁定在 composer.lock 中。此包不会新增任何安装期脚本。请见 /integrations/codeigniter/security-and-operations/.

  • Composer 发现机制依赖 PSR-4 自动加载。
  • /integrations/codeigniter/overview/ — 此包提供的功能。
  • /integrations/codeigniter/quickstart/ — 在控制器中生成第一份 PDF。
  • /integrations/codeigniter/configuration/ — 配置键与覆盖设置。
  • /integrations/codeigniter/boot-and-discovery/ — 发现流程的详细说明。