Skip to content

Quickstart — your first PDF in CodeIgniter 4

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.

Terminal window
composer require nextpdf/codeigniter

For requirements and package discovery checks, see /integrations/codeigniter/install/.

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.

Map a route to the controller method in app/Config/Routes.php:

$routes->get('invoices/(:num)/pdf', 'InvoiceController::download/$1');

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');
}
}
  • A controller that resolves a NextPDF service and returns a typed DownloadResponse.
  • Two equivalent entry points: the Services class and the pdf() / pdf_document() helpers.
  • A response with application/pdf, the package security headers, and a sanitized filename.

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.

  • 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).
  • /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.