Skip to content

Your first PDF

In this tutorial you generate two PDF files. First, you write text directly with the fluent core Application Programming Interface (API). Then you render a Hypertext Markup Language (HTML) fragment with Cascading Style Sheets (CSS) into a page. By the end, you have a working script you can adapt for a real document, plus links to the framework and server variants.

Both approaches run on the same engine, so what you learn here carries across every distribution.

The diagram below shows the two authoring approaches and the three deployment surfaces that reuse them.

From your code to a PDF fileTwo authoring approaches feed one engine, which produces a PDF that you can save, stream, or return from a framework or server.

Authoring

Fluent API

setFont() + cell()

HTML fragment

writeHtml()

Document

Core engine

(pure PHP)

PDF 2.0 bytes

save() to disk

output() / getPdfData()

stream or in memory

Framework response

or server REST reply

From your code to a PDF file

You need the core engine installed and a place to run a PHP script.

  1. Confirm PHP 8.4 is the active runtime:

    Terminal window
    php --version
  2. Install the core engine in your project, if it is not already present:

    Terminal window
    composer require nextpdf/core
  3. Create a working file named first-pdf.php in the project root.

Start with the fluent API. Document::createStandalone() returns a document you can use right away. Add a page, set a font, write cells, then save. Each authoring method returns the document, so the calls read from top to bottom.

  1. Put this code in first-pdf.php. The script declares strict types, loads the autoloader, and builds one page:

    <?php
    declare(strict_types=1);
    require_once __DIR__ . '/vendor/autoload.php';
    use NextPDF\Core\Document;
    $document = Document::createStandalone();
    $document->setTitle('My first NextPDF document');
    $document->addPage();
    $document->setFont('helvetica', 'B', 24);
    $document->cell(0, 15, 'Hello, NextPDF!', newLine: true);
    $document->setFont('helvetica', '', 12);
    $document->cell(0, 10, 'This is the first PDF I generated with PHP.', newLine: true);
    $document->save(__DIR__ . '/first-pdf.pdf');
    echo "Wrote first-pdf.pdf\n";
  2. Run the script:

    Terminal window
    php first-pdf.php
  3. Confirm the output. You should see this line on standard output and a new file named first-pdf.pdf next to the script:

    Wrote first-pdf.pdf

You have now generated a valid PDF 2.0 file. Open it in any viewer to see the heading and the line below it.

Writing cells gives you precise control. Most documents, however, are faster to express as HTML and CSS. The core engine includes a pure-PHP HTML pipeline. Its writeHtml() method renders a fragment into the current page. It does not use a browser or an external service.

  1. Create a second file, html-pdf.php, that renders an HTML fragment:

    <?php
    declare(strict_types=1);
    require_once __DIR__ . '/vendor/autoload.php';
    use NextPDF\Core\Document;
    $document = Document::createStandalone();
    $document->setTitle('HTML to PDF');
    $document->addPage();
    $html = <<<'HTML'
    <h1 style="color: #1E3A8A;">HTML rendering in NextPDF</h1>
    <p>NextPDF renders <strong>HTML content</strong> directly into PDF pages.</p>
    <ul>
    <li>Headings, paragraphs, and lists</li>
    <li>Inline <strong>bold</strong> and <em>italic</em></li>
    <li>Inline styles such as color and font-size</li>
    </ul>
    HTML;
    $document->writeHtml($html);
    $document->save(__DIR__ . '/html-pdf.pdf');
    echo "Wrote html-pdf.pdf\n";
  2. Run it:

    Terminal window
    php html-pdf.php
  3. Confirm the output line and the new file:

    Wrote html-pdf.pdf

The engine renders a supported subset of HTML and CSS. Before you rely on a property, check the CSS support matrix. When a layout needs full browser fidelity, such as flexbox, grid, or web fonts, install the Artisan renderer and call writeHtmlChrome() instead. That method keeps text selectable.

Your application may already run in a framework or through the server. The same two calls move into that environment. The engine stays the same. Only the wiring changes.

The Pdf facade resolves a fresh document for each call. PdfResponse turns a document into a download response:

<?php
declare(strict_types=1);
namespace App\Http\Controllers;
use Illuminate\Http\Response;
use NextPDF\Contracts\PdfDocumentInterface;
use NextPDF\Laravel\Http\PdfResponse;
final class ReportController extends Controller
{
public function download(): Response
{
$document = app(PdfDocumentInterface::class);
$document->addPage();
$document->cell(0, 10, 'Monthly report', newLine: true);
return PdfResponse::download($document, 'report.pdf');
}
}

See the Laravel quickstart.

You have now generated a PDF three ways. Use these pages to build on each one.