Render a PDF

Render a page to a PDF file and to bytes.

This example renders example.com to a PDF file, then renders the same page to in-memory bytes and prints the byte count.

Use it when PDF output is part of the product, such as invoices, reports, receipts, or printable pages.

Run it

bash
php content/examples/pdf.php

Expected output:

text
PDF saved to: .../var/playwright-artifacts/examples/example.pdf
Inline PDF bytes: ...

The byte count varies by browser version and rendering details.

What it demonstrates

  • pdf() writes a PDF file.
  • pdfContent() returns the generated PDF as bytes.
  • Print output should be generated after the page reaches the state you want to print.

Upload or clean the artifact directory according to your test policy.

Go next

Source

pdf.php
php
<?php

declare(strict_types=1);

/*
 * This file is part of the community-maintained Playwright PHP project.
 * It is not affiliated with or endorsed by Microsoft.
 *
 * (c) 2025-Present - Playwright PHP - https://github.com/playwright-php
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

require_once __DIR__.'/../../vendor/autoload.php';

use Playwright\Playwright;

$artifactDir = __DIR__.'/../../var/playwright-artifacts/examples';
is_dir($artifactDir) || mkdir($artifactDir, 0777, true);

$context = Playwright::chromium();
$page = $context->newPage();

$page->goto('https://example.com');

$pdfPath = $artifactDir.'/example.pdf';
$page->pdf($pdfPath, ['format' => 'A4']);
echo 'PDF saved to: '.$pdfPath."\n";

$pdfBytes = $page->pdfContent();
echo 'Inline PDF bytes: '.strlen($pdfBytes)."\n";

$page->close();
$context->close();