Convert a URL to a PDF in PHP with Dompdf
Fetch a web page and render it as a PDF with Dompdf in PHP. Learn remote resource setup, security limits, troubleshooting, and when browser capture is a better fit.
Dompdf can turn a URL into a PDF from PHP: install it with Composer, enable remote resources only when needed, load the page with loadHtmlFile(), render it, then stream or save the result. It renders HTML and the CSS it supports; it does not run a full browser, so a page’s PDF may differ from its browser appearance.
1. Install Dompdf
From your PHP project directory, install the package:
composer require dompdf/dompdf
Load Composer’s autoloader in the PHP script that creates the PDF. Check the documentation for the version installed in your project because the repository README can describe code newer than a released package.
2. Fetch a URL and stream the PDF
This complete example loads a public page, renders it on A4 paper in portrait orientation, and sends the PDF to the browser:
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use Dompdf\Dompdf;
use Dompdf\Options;
$url = 'https://example.com/page';
$options = new Options();
// Enable this only when the page needs assets such as remote images or CSS.
$options->set('isRemoteEnabled', true);
$dompdf = new Dompdf($options);
$dompdf->loadHtmlFile($url);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream('page.pdf', ['Attachment' => true]);
Replace the example URL with the page you intend to convert. The URL must be reachable from the PHP server. To display the PDF in the browser instead of prompting a download, use ['Attachment' => false].
3. Save the PDF to a file
If the application needs to store or process the PDF, use output() after rendering and write the returned bytes to a controlled destination:
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use Dompdf\Dompdf;
use Dompdf\Options;
$options = new Options();
$options->set('isRemoteEnabled', true);
$dompdf = new Dompdf($options);
$dompdf->loadHtmlFile('https://example.com/page');
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$pdfBytes = $dompdf->output();
$destination = __DIR__ . '/generated/page.pdf';
if (file_put_contents($destination, $pdfBytes) === false) {
throw new RuntimeException('Could not write the PDF file.');
}
Ensure the destination directory exists and is writable by the PHP process. Choose a filename and path in application code rather than allowing an untrusted request value to select an arbitrary filesystem location.
4. Configure remote resources safely
Dompdf disables remote resource access by default. Set isRemoteEnabled to true when the document needs to fetch remote images or stylesheets. Remote fetching also requires PHP cURL or allow_url_fopen to be available.
Leave remote access disabled when it is unnecessary. If you enable it for URL conversion, treat the target URL as a security boundary: do not accept arbitrary user-supplied URLs without validation and network restrictions. A renderer that fetches URLs can otherwise be exposed to requests for sensitive internal services. Where the installed version and configuration allow it, restrict which remote hosts can be accessed.
Local file references are constrained by Dompdf’s configured chroot paths. Keep that boundary narrow; do not widen it casually to make a missing asset work. Embedded PHP is disabled by default. Keep it disabled when documents or markup may be untrusted.
5. Choose paper size and orientation
setPaper() accepts a paper size and an orientation. For example:
$dompdf->setPaper('A4', 'portrait');
// Or:
$dompdf->setPaper('letter', 'landscape');
Set the paper before calling render(). Confirm the page dimensions and orientation with the intended output format, especially when converting content designed for a wide screen.
6. Understand layout and fidelity limits
Dompdf supports much of CSS 2.1 and selected CSS3 features, but it is not a browser engine. In particular, Flexbox and CSS Grid are unsupported, table rows cannot split across pages, and raw inline SVG has restrictions. If a page relies on those features, simplify the markup and styles for PDF output or use a browser-based rendering approach.
For reliable pagination, prefer straightforward HTML and print-oriented CSS, keep tables manageable, and verify that required images and styles are available to the PHP process. Do not promise an exact match to the live page: the result depends on the HTML, styles, resources, and layout features Dompdf can parse.
7. Troubleshooting
| Symptom | Likely cause | What to check |
|---|---|---|
| Remote images or CSS are missing | Remote fetching is disabled, or PHP lacks a supported URL-fetching mechanism. | Enable isRemoteEnabled only if required, and confirm cURL or allow_url_fopen is available to the PHP runtime. |
| Local images or stylesheets do not load | The referenced path is outside the configured chroot. |
Use paths within the allowed directory and review the chroot configuration without widening it unnecessarily. |
| The PDF layout differs from the browser | The page uses unsupported layout features or browser behavior Dompdf does not implement. | Check for Flexbox, Grid, table pagination constraints, and inline SVG; simplify the PDF markup or use browser-based capture. |
| The remote page does not load | The PHP server cannot reach the URL, or remote access is not configured. | Check server network access, URL correctness, PHP URL-fetching support, and the remote-resource setting. |
| The output file cannot be written | The target directory is missing or not writable by the PHP process. | Create a controlled destination directory and verify its filesystem permissions. |
| Results appear to carry over between documents | A Dompdf instance has been reused. | Create a new Dompdf instance for each document; parser and rendering artifacts may persist across renders. |
8. Reliability, security, and cost considerations
- Use a fresh renderer per document. The Dompdf README warns that parser and rendering artifacts can persist, so do not reuse one instance across documents.
- Keep inputs controlled. Untrusted URLs and HTML combined with remote or local resource access can expose sensitive services or files. Keep embedded PHP disabled for untrusted content.
- Check the installed release. Review current security advisories and the documentation for the exact package version deployed. The official advisory index lists multiple July 20, 2026 advisories and identifies Dompdf 3.1.6 as patched for several listed issues; check the index for current applicability and fixes.
- Budget for rendering work. PDF generation consumes application resources and may fetch remote assets. Bound inputs and request duration according to your application’s needs; the research sources do not establish a universal performance benchmark.
- Account for dependencies. Dompdf is Composer-installed software. Its direct price is not specified in the cited documentation; operational cost depends on your PHP hosting and workload.
Official references: Dompdf README and project documentation, Dompdf options source, and the security advisory index.
9. Or skip the browser setup
Dompdf is useful when you want to render HTML in PHP and can work within its layout support. If your goal is a browser-rendered capture as a PDF, ScreenshotNeo is a website screenshot API and MCP server for developers. Its one-request API returns a PDF, and the response identifies page verdict and billing status.
See the ScreenshotNeo API documentation. Replace the target URL below as needed:
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -d format=pdf -o page.pdf
ScreenshotNeo accepts cookie and consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each step can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits cost nothing. Its MCP server provides screenshot tools for AI agents. The free plan includes 1,000 shots a month with no card; paid plans start at $5 for 3,000 shots.
Sign up for 1,000 free screenshots a month, no card required.
10. FAQ
Can Dompdf convert any public URL?
It can load a URL, but the page and its resources must be reachable from the PHP server, and the page must be compatible with Dompdf’s renderer and configuration.
Does Dompdf produce the same output as Chrome or Firefox?
No exact browser match is established. Dompdf has its own HTML and CSS support, including the limitations described above.
Should I reuse one Dompdf instance for a batch?
No. Create a new instance for each document because rendering artifacts may persist across renders.
When should I choose a browser capture API instead?
Consider one when the required output depends on browser layout behavior or you want a browser-rendered capture workflow. For a PHP HTML-to-PDF renderer with supported markup, Dompdf remains a direct option.


