Quickstart — your first PDF in CodeIgniter 4
At a glance
Section titled “At a glance”Resolve the pdf service in a controller, add content to its document,
and return a download response. That gives you a Portable Document
Format (PDF) file in three lines and one Hypertext Transfer Protocol
(HTTP) response.
Install
Section titled “Install”composer require nextpdf/codeigniterFor requirements and package discovery checks, see /integrations/codeigniter/install/.
Step 1 — Generate a controller
Section titled “Step 1 — Generate a controller”Create a controller that returns a PDF. The Pdf library creates a
fresh document for you, then turns that document into a CodeIgniter
DownloadResponse.
Every PHP sample on this page puts declare(strict_types=1); after
the opening tag, on its own line (PSR-12 §x1.x3.p11;
§x1.x3.p34).
<?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"); }}Services::pdf() returns a fresh Pdf. The package’s functional test
suite verifies that its underlying document is fresh on every call too.
The download() call produces a DownloadResponse with the
attachment disposition.
Step 2 — Add a route
Section titled “Step 2 — Add a route”Map a route to the controller method in app/Config/Routes.php:
$routes->get('invoices/(:num)/pdf', 'InvoiceController::download/$1');Step 3 — Request the PDF
Section titled “Step 3 — Request the PDF”Open /invoices/42/pdf. Your browser downloads invoice-42.pdf. The
response includes Content-Type: application/pdf and the package’s
response-hardening headers.
Variation — inline preview with the helper
Section titled “Variation — inline preview with the helper”The global pdf() helper is equivalent to Services::pdf(). Call
inline() to show the PDF in the browser instead of downloading it:
<?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'); }}Variation — direct document and PdfResponse
Section titled “Variation — direct document and PdfResponse”If you already have a Document, build the response directly with
PdfResponse. The pdf_document() helper gives you a fresh,
pre-configured document.
<?php
declare(strict_types=1);
namespace App\Controllers;
use CodeIgniter\HTTP\DownloadResponse;use NextPDF\CodeIgniter\Http\PdfResponse;
final class DocumentController extends BaseController{ public function generate(): DownloadResponse { $document = pdf_document(); $document->addPage(); $document->cell(0, 10, 'Hello World');
return PdfResponse::download($document, 'hello.pdf'); }}What you built
Section titled “What you built”- A controller that resolves a NextPDF service and returns a typed
DownloadResponse. - Two equivalent entry points: the
Servicesclass and thepdf()/pdf_document()helpers. - A response with
application/pdf, the package security headers, and a sanitized filename.
Next steps
Section titled “Next steps”To keep this first introduction focused, this tutorial leaves out error handling. For production controllers with dependency injection, exception handling, observability, and the queue job, continue to /integrations/codeigniter/production-usage/. That page shows you the hardened variants.
Conformance
Section titled “Conformance”- PHP opening tag on its own line in code samples (PSR-12 Extended Coding Style §x1.x3.p11).
- declare(strict_types=1) statement form in code samples (PSR-12 Extended Coding Style §x1.x3.p34).
See also
Section titled “See also”- /integrations/codeigniter/overview/ — the full application programming interface (API) surface.
- /integrations/codeigniter/configuration/ — change defaults and paths.
- /integrations/codeigniter/production-usage/ — production-grade controllers and asynchronous jobs.
- /integrations/codeigniter/troubleshooting/ — when a route returns no PDF.