Troubleshooting NextPDF on CodeIgniter 4
At a glance
Section titled “At a glance”Each symptom below maps to a verified cause in the package or framework source and includes a concrete fix.
Discovery and resolution
Section titled “Discovery and resolution”Services::pdfDocument() returns null
Section titled “Services::pdfDocument() returns null”When CodeIgniter resolves a service, it scans the discovered Config\Services
classes for a matching method. A null return means CodeIgniter did not
discover the package’s Services class.
Check these causes and fixes:
- Auto-discovery is disabled. The host application may set
Config\Modules::$discoverInComposer = false. If so, addnextpdf/codeigniterto$composerPackages['only']. CodeIgniter scans Composer packages only when this flag istrue. - The autoloader is stale. Composer maps the namespace prefix
NextPDF\CodeIgniter\to its base directory. A stale classmap hides the class (PSR-4 §x1.x3). Runcomposer dump-autoload. - The
$aliaseslist was trimmed. Discovery runs only for the entries inConfig\Modules::$aliases. The package needsservicesand, for helpers,registrars. Restore both entries.
pdf() or pdf_document() is undefined
Section titled “pdf() or pdf_document() is undefined”The helpers load through two routes: the package Composer files
autoload entry and the package Registrar. An undefined-function error
means the files entry did not load.
- Run
composer dump-autoloadto rebuild thefilesautoload list. - Confirm that
nextpdf/codeigniterappears invendor/composer/autoload_files.php. - If you need a workaround, call
Services::pdf(false)orServices::pdfDocument(false)directly. The helpers are thin wrappers around these calls.
Configuration
Section titled “Configuration”.env overrides are ignored
Section titled “.env overrides are ignored”To resolve an override, BaseConfig uses the lowercase short class name
as the prefix. Because the class is NextPdf, the prefix is nextpdf.
It is not nextPdf or NextPdf.
- Use
nextpdf.fontsPath, notnextPdf.fontsPath. - For a nested key, use a dot:
nextpdf.signature.certificate. - The fully qualified form
NextPDF\CodeIgniter\Config\NextPdf.fontsPathalso works.
A whole config array reverts to defaults
Section titled “A whole config array reverts to defaults”When you extend the NextPdf class and assign a partial array, CodeIgniter
replaces the whole array. Provide every key in the array you override.
For a full-array example, see /integrations/codeigniter/configuration/.
Runtime errors
Section titled “Runtime errors”RuntimeException: NextPDF requires the ext-… PHP extension
Section titled “RuntimeException: NextPDF requires the ext-… PHP extension”The font registry validates mbstring and zlib once per process. It
raises this error with the missing extension name. Install or enable the
named extension in the runtime PHP, then restart the worker or PHP FastCGI
Process Manager (PHP-FPM) pool.
RuntimeException: NextPdf fontsPath contains invalid characters
Section titled “RuntimeException: NextPdf fontsPath contains invalid characters”The font registry rejects a fontsPath that contains a stream wrapper
(://) or a null byte. Set fontsPath to a plain file system path. Do
not point it at a php://, phar://, or similar wrapped path.
Response problems
Section titled “Response problems”Filename looks wrong in the download
Section titled “Filename looks wrong in the download”PdfResponse sanitizes filenames. Expect this verified behavior:
- An empty or whitespace-only filename becomes
document.pdf. - A name without a
.pdf(or.PDF) extension gets.pdfappended. An existing.PDFis preserved as-is. - A name with non-ASCII characters produces an ASCII fallback and an RFC 5987
filename*=UTF-8''…parameter, so modern browsers show the original name. This is expected, not a bug. - Path separators, null bytes, and carriage return/line feed (CR/LF) are stripped.
Response missing security headers
Section titled “Response missing security headers”Every PdfResponse includes X-Content-Type-Options,
X-Frame-Options, Content-Security-Policy, X-Robots-Tag, and
Referrer-Policy. If they are absent at the client, a proxy or the
application is stripping or overwriting them downstream. Inspect the
response both before and after your reverse proxy.
QueueException when pushing the job
Section titled “QueueException when pushing the job”The queue checks the pushed job name against the keys in
Config\Queue::$jobHandlers and rejects any name that is not
registered. Register the job under a name key, then push that name:
public array $jobHandlers = ['generate-pdf' => GeneratePdfJob::class];
// dispatch\service('queue')->push('pdf-queue', 'generate-pdf', [...]);Pushing GeneratePdfJob::class as the job name fails. The second
argument is a name key, not a class string.
InvalidArgumentException from the job
Section titled “InvalidArgumentException from the job”The job validates its payload before doing any work. These verified rejection cases return these message fragments:
| Cause | Message fragment |
|---|---|
builder missing, empty, or not a string | non-empty static callable string |
builder outside App\PdfBuilders | not allowed |
builder matches the pattern but is not callable | not a valid callable |
outputPath missing or empty | non-empty string |
outputPath outside WRITEPATH/pdfs/ | outside of allowed directory |
outputPath not ending in .pdf | must end with .pdf |
Fix the payload so the builder is an
App\PdfBuilders\<Class>::<method> static callable. Make sure the
output path resolves inside WRITEPATH/pdfs/ with a .pdf extension.
class … BaseJob not found
Section titled “class … BaseJob not found”Because codeigniter4/queue is a development-only dependency of the package,
the application that runs workers must require it directly:
composer require codeigniter4/queueDiagnostics
Section titled “Diagnostics”composer show nextpdf/codeigniter— confirm Composer resolved the package.composer dump-autoload— rebuild discovery and the helper autoload list.php spark routes— confirm your PDF routes are registered.- For the fastest discovery check, use a controller that calls
Services::pdfDocument(false)and asserts the result is aDocument.
Conformance
Section titled “Conformance”- Class-to-path mapping — relevant to discovery failures (PSR-4 Autoloader §x1.x3).
See also
Section titled “See also”- /integrations/codeigniter/install/ — requirements for discovery.
- /integrations/codeigniter/configuration/ — the
.envprefix and array-override rule. - /integrations/codeigniter/production-usage/ — correct queue registration.
- /integrations/codeigniter/boot-and-discovery/ — the discovery sequence.