Skip to content

CodeIgniter API reference

The CodeIgniter package exposes a small controller-facing application programming interface (API). It includes a Pdf library wrapper for one document (Services::pdf() and the global pdf() helper), response helpers that turn a built document into a DownloadResponse (PdfResponse), the Services factories behind them (font and image registries, the document factory, and optional signer and time-stamping authority (TSA) client), the NextPdf configuration class, and GeneratePdfJob for asynchronous generation from static builder callables.

Start with the main controller path: call Services::pdf() (or the pdf() helper), add content to $pdf->document(), and return $pdf->download('file.pdf'). That path covers the common case. The reference tables are organized by surface; Common tasks shows the runnable shapes first.

These are the most common production flows. Each sample is source-verified against nextpdf/codeigniter.

Use the canonical render path to return a downloadable Portable Document Format (PDF) file from a controller:

<?php
declare(strict_types=1);
namespace App\Controllers;
use CodeIgniter\HTTP\DownloadResponse;
use NextPDF\CodeIgniter\Config\Services;
final class InvoiceController extends BaseController
{
public function download(int $id): DownloadResponse
{
$pdf = Services::pdf();
$pdf->document()->addPage();
$pdf->document()->cell(0, 10, "Invoice #{$id}");
return $pdf->download("invoice-{$id}.pdf");
}
}

What it does: resolves a fresh Pdf around a fresh Document, writes one cell, and returns a DownloadResponse with attachment disposition and the package security headers.

Preview a PDF inline in the browser with the same wrapper and inline() instead of download():

<?php
declare(strict_types=1);
namespace App\Controllers;
use CodeIgniter\HTTP\DownloadResponse;
final class ReportController extends BaseController
{
public function preview(): DownloadResponse
{
$pdf = pdf();
$pdf->document()->addPage();
$pdf->document()->cell(0, 10, 'Monthly Report');
return $pdf->inline('report.pdf');
}
}

What it does: uses the global pdf() helper (equivalent to Services::pdf()) and returns a DownloadResponse with inline disposition so the browser displays the PDF instead of downloading it.

Generate a PDF asynchronously on the queue with GeneratePdfJob and a static builder:

<?php
declare(strict_types=1);
use NextPDF\CodeIgniter\Jobs\GeneratePdfJob;
service('queue')->push('pdf', GeneratePdfJob::class, [
'builder' => 'App\PdfBuilders\InvoiceBuilder::build',
'outputPath' => WRITEPATH . 'pdfs/invoice-42.pdf',
'context' => ['invoice_id' => 42],
]);

What it does: enqueues generation. The worker validates the builder (must be an App\PdfBuilders\...::method static callable) and the output path (must resolve under WRITEPATH/pdfs/ and end in .pdf), builds a fresh document, and saves it.

Use this table when you have a Pdf wrapper and need its response or save methods.

SymbolParametersDefault behaviorReturnsThrows or fails withNotes
NextPDF\CodeIgniter\Libraries\Pdf / new Pdf(Document $document)document: fresh core document.Wraps the supplied document for one response or save operation.PdfNone expected.Do not reuse the wrapper after output.
Pdf::document()none.Returns the wrapped document.NextPDF\Core\DocumentNone expected.Use this to call core authoring APIs.
Pdf::inline(string $filename = 'document.pdf')filename: response filename.Uses browser inline disposition.CodeIgniter\HTTP\DownloadResponseCore serialization errors.Delegates to PdfResponse::inline().
Pdf::download(string $filename = 'document.pdf')filename: response filename.Uses browser attachment disposition.DownloadResponseCore serialization errors.Delegates to PdfResponse::download().
Pdf::streamInline(string $filename = 'document.pdf')filename: response filename.Provides API parity with other framework packages.DownloadResponseCore serialization errors.CodeIgniter handles binary output natively.
Pdf::streamDownload(string $filename = 'document.pdf')filename: response filename.Provides API parity with other framework packages.DownloadResponseCore serialization errors.Use the same size controls as non-streamed responses.
Pdf::save(string $path)path: filesystem target.Writes the wrapped document.voidFilesystem or core write errors.Validate storage roots before saving.

Use this table when you need a specific service factory or global helper function, including its sharing behavior and return type.

SymbolParametersDefault behaviorReturnsThrows or fails withNotes
Services::fontRegistry(bool $getShared = true)getShared: CodeIgniter shared-service flag.Returns a shared registry, warms it with configured fonts, then locks it.FontRegistryInterfaceRuntimeException for missing extensions or unsafe font path.Rejects stream wrappers and null bytes in fontsPath.
Services::imageRegistry(bool $getShared = true)getShared: shared-service flag.Returns a shared bounded least recently used (LRU) image registry.ImageRegistryNone expected.Cache size is controlled by imageCacheMb.
Services::documentFactory(bool $getShared = true)getShared: shared-service flag.Returns a shared factory that uses shared registries.DocumentFactoryInterfaceRegistry setup errors.The factory is reusable; documents are not.
Services::tsaClient(bool $getShared = true)getShared: shared-service flag.Returns null when tsa.url is empty.`TsaClientnull`Hypertext Transfer Protocol (HTTP) client or TSA configuration errors.
Services::pdfSigner(bool $getShared = false)getShared: shared-service flag.Returns null when signing is disabled.`SignerInterfacenull`Certificate or signature-level errors.
Services::pdfDocument(bool $getShared = false)getShared: shared-service flag.Creates a fresh document, applies defaults, and optionally configures PDF/A or Artisan.DocumentOptional extension or document configuration errors.Keep the default false for request safety.
Services::pdf(bool $getShared = false)getShared: shared-service flag.Creates a fresh Pdf wrapper around a fresh document.NextPDF\CodeIgniter\Libraries\PdfDocument setup errors.Main controller-facing service.
Services::eInvoiceEmbedder()none.Returns null unless the Premium Pro e-invoice embedder class exists.`EmbedderInterfacenull`Optional package construction errors.
Services::eInvoiceValidator()none.Returns null unless the Premium Enterprise validator class exists.`ValidatorInterfacenull`Optional package construction errors.
Services::eInvoiceProfile()none.Returns the EN16931 profile when Premium Pro is installed.`ProfileInterfacenull`Optional package errors.
Services::schematronRunner()none.Returns null unless the Premium Enterprise Schematron validator exists.`SchematronRunnerInterfacenull`Optional package construction errors.
Registrar::Autoload()none.Adds the package helper to CodeIgniter autoload configuration.arrayNone expected.Enables pdf() and pdf_document() when the module is loaded.
pdf()none.Calls Services::pdf(false).PdfDocument setup errors.Convenience helper for controllers.
pdf_document()none.Calls Services::pdfDocument(false).DocumentDocument setup errors.Convenience helper when the core document API is preferred.

Use this table when you already have a built Document and want to construct the DownloadResponse yourself instead of using the Pdf wrapper.

SymbolParametersDefault behaviorReturnsThrows or fails withNotes
PdfResponse::inline(Document $document, string $filename = 'document.pdf')document: built document; filename: response filename.Ensures the .pdf extension and inline disposition.DownloadResponseCore serialization errors.Adds the PDF content type and defensive headers.
PdfResponse::download(Document $document, string $filename = 'document.pdf')Same as inline; disposition is attachment.Ensures .pdf extension.DownloadResponseSame as inline.Use for browser downloads.
PdfResponse::streamInline(Document $document, string $filename = 'document.pdf')Same as inline.Same behavior as inline in CodeIgniter 4 (CI4).DownloadResponseSame as inline.Exists for cross-framework API parity.
PdfResponse::streamDownload(Document $document, string $filename = 'document.pdf')Same as download.Same behavior as download in CI4.DownloadResponseSame as download.Exists for cross-framework API parity.

Use this table when you wire asynchronous generation and need the exact job data keys and builder-callable contract.

SymbolParametersDefault behaviorReturnsThrows or fails withNotes
GeneratePdfJob::process()Job data keys: builder, outputPath, and optional context.Uses an empty context array when omitted.voidInvalidArgumentException for unsafe builder or output path; core write errors.Builder must be App\PdfBuilders\...\*::method.
Builder callableDocument $doc, array $context.No default context beyond job data.DocumentBuilder-specific exceptions.Requires a static callable because CI4 queue payloads are serialized data.

Use this table when you change defaults for page format, paths, signing, TSA, or document metadata on the NextPdf config class.

PropertyTypeDefault behaviorNotes
pageFormatstringA4.Sets the default page format.
orientationstringP.Sets the default orientation.
unitstringmm.Sets the default unit.
pdfa`stringnull`null.
fontsPath / cachePathstringWRITEPATH . 'fonts' and WRITEPATH . 'cache/nextpdf'.Keep paths inside application-controlled storage.
signaturearrayDisabled with level B-B.Certificate, key, password, extra certificates, and level.
tsaarrayDisabled when the Uniform Resource Locator (URL) is null; timeout 30 seconds.Credentials, mutual Transport Layer Security (mTLS) files, public-key pins, and HTTP policy.
ocspCachearrayEnabled with a time to live (TTL) of 86400 seconds.Used by signature validation flows when available.
preloadFontslist<string>Empty.Warmed before the registry is locked.
imageCacheMbint50.Controls the process-lifetime image cache.
fontCacheLockingbooltrue.Keeps font registry mutations out of request handling.
artisanarrayChrome renderer disabled unless configured and installed.Maps to ChromeRendererConfig::fromArray().
defaultsarrayCreator NextPDF, empty author, language en, default margins, and default font.Services::pdfDocument() applies only creator, language, and (when non-empty) author; margin_top/right/bottom/left, font_family, font_size, trim_box, and bleed_box are defined defaults, but it does not currently apply them.
  • GeneratePdfJob confines output to WRITEPATH . 'pdfs' and requires .pdf.
  • Builder callables outside App\PdfBuilders are rejected to avoid arbitrary code execution from modified queue payloads.
  • Use service('pdf') or the package helper for controller flows; use queue jobs for long-running generation.