Your first PDF
At a glance
Section titled “At a glance”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.
Before you begin
Section titled “Before you begin”You need the core engine installed and a place to run a PHP script.
-
Confirm PHP 8.4 is the active runtime:
Terminal window php --version -
Install the core engine in your project, if it is not already present:
Terminal window composer require nextpdf/core -
Create a working file named
first-pdf.phpin the project root.
Generate a PDF from PHP
Section titled “Generate a PDF from PHP”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.
-
Put this code in
first-pdf.php. The script declares strict types, loads the autoloader, and builds one page:<?phpdeclare(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"; -
Run the script:
Terminal window php first-pdf.php -
Confirm the output. You should see this line on standard output and a new file named
first-pdf.pdfnext 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.
Render HTML to PDF
Section titled “Render HTML to PDF”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.
-
Create a second file,
html-pdf.php, that renders an HTML fragment:<?phpdeclare(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"; -
Run it:
Terminal window php html-pdf.php -
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.
From a framework controller
Section titled “From a framework controller”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.
The bundle exposes a PdfFactory service. Type-hint
NextPDF\Symfony\Service\PdfFactory in a controller, build a document the
same way, and return it as an HTTP response. See the
Symfony quickstart.
With NextPDF Server running, POST an ordered operations array, and the server returns the PDF bytes:
curl -sS -X POST http://localhost:8080/api/v1/render \ -H 'Authorization: Bearer <api-key>' \ -H 'Content-Type: application/json' \ -d '{ "page_size": "A4", "orientation": "portrait", "operations": [ { "type": "add_text", "text": "Hello from NextPDF Connect" } ] }' \ --output hello.pdfOn a 200 response, the body is the PDF. See the
Connect quickstart.
Next steps
Section titled “Next steps”You have now generated a PDF three ways. Use these pages to build on each one.