What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To convert HTML to PDF in PHP, load the markup into a PDF renderer, render it, then either send the PDF to the browser or save its bytes to a file. This tutorial uses Dompdf: install it with Composer, call loadHtml(), optionally set the paper size, call render(), and use stream() or output(). The example is suitable for straightforward reports and invoices; it does not promise that every modern browser layout will render identically.

Video tutorial

For a visual walkthrough, see the CodexWorld video tutorial, “Convert HTML to PDF in PHP with Dompdf”, published November 15, 2022. The runnable example below shows the same basic conversion flow using Dompdf.

Install Dompdf and check your environment

In a Composer-managed project, install Dompdf from the project directory:

composer require dompdf/dompdf

Load Composer’s generated autoloader in your PHP entry point. Check the requirements for the exact Dompdf release you install: the project lists PHP 7.1 or newer, the DOM extension, php-font-lib, and php-svg-lib; MBString and GD are also relevant to processing support. Dependencies can have their own requirements, and compatibility can change by release. See the Dompdf project repository before deploying.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

Generate and deliver a PDF

This complete example creates a small HTML document, renders it as US Letter portrait, and sends the PDF to the browser. Save it as a PHP file served by your application, after installing Dompdf:

<?php
require __DIR__ . '/vendor/autoload.php';

use DompdfDompdf;
use DompdfOptions;

$options = new Options();
// Remote access is off by default. Leave it off unless the document needs
// to load remote images or stylesheets; see the security section below.
$dompdf = new Dompdf($options);

$html = '<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Monthly report</title>
  <style>
    body { font-family: DejaVu Sans, sans-serif; font-size: 12px; }
    h1 { color: #174ea6; }
    table { width: 100%; border-collapse: collapse; }
    th, td { border: 1px solid #999; padding: 6px; text-align: left; }
  </style>
</head>
<body>
  <h1>Monthly report</h1>
  <p>Generated by a PHP application.</p>
  <table>
    <tr><th>Item</th><th>Amount</th></tr>
    <tr><td>Example service</td><td>$25.00</td></tr>
  </table>
</body>
</html>';

$dompdf->loadHtml($html, 'UTF-8');
$dompdf->setPaper('letter', 'portrait'); // Optional; defaults can be used instead.
$dompdf->render();
$dompdf->stream('monthly-report.pdf', ['Attachment' => true]);

Attachment => true asks the browser to download the PDF. Set it to false to request inline display, subject to browser behavior. Do not print debug text, warnings, or whitespace before the PDF response: extra output can corrupt the file.

Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

Save the rendered PDF instead

If the application needs to store the result, replace the stream() call with output() and write the returned bytes to a controlled destination:

$pdfBytes = $dompdf->output();
file_put_contents(__DIR__ . '/monthly-report.pdf', $pdfBytes);

In a real application, choose a destination with appropriate access controls and handle write failures. Avoid using a user-provided filename or path without validation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.

Paper size, orientation, and document layout

setPaper() is optional. The example uses letter and portrait; other common print choices include A4 and landscape. Set the page format before render(). Your HTML and CSS should be designed for pages rather than assumed to behave exactly like a browser viewport. Dompdf documents mostly CSS 2.1 support with selected CSS3 behavior, including some @import, @media, and @page support. Test the actual document’s fonts, tables, images, and page breaks with the Dompdf release you deploy; its support description is not a guarantee of modern browser layout parity. Details are in the Dompdf tutorial.

Choose a PHP HTML-to-PDF renderer

Dompdf is one option, not a universal best choice. Compare renderers against the document’s layout requirements and the server environment. The cited project documentation describes capabilities; it does not establish a common benchmark or prove that one renderer is faster or more accurate for your page.

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
Renderer What its documentation describes Consider it when
Dompdf PHP-based rendering, mostly CSS 2.1 with some CSS3 behavior; external stylesheets and images are subject to configuration and access restrictions. You need a PHP library for conventional print-style pages and can work within its rendering and security model. Project and requirements.
mPDF Accepts HTML and custom tags as well as PHP commands. Its project describes headers and footers, page numbering, tables of contents, barcodes, and print-oriented color handling. Those document features fit your output. The manual recommends HTML/CSS for newcomers and warns that outside HTML/CSS needs vetting and sanitization. HTML or PHP guidance · First-file example and safety warning · Project and runtime details.
tc-lib-pdf The current TCPDF project describes HTML/CSS rendering through addHTMLCell() and getHTMLCell(); its engine renders a subset itself rather than delegating to a browser engine, and an HTML cell can flow across pages. You need its documented PDF features and can build within its HTML/CSS subset. Consult the current workflow rather than relying only on legacy writeHTML() examples. Project overview · HTML/CSS guide.
wkhtmltox The PHP manual documents interfaces to a library that renders HTML to PDF and image formats using QtWebKit. Your deployment can accommodate the external library and its integration. This is a different deployment shape from a pure-PHP renderer. PHP manual.

Before choosing, verify the selected release’s PHP version and extension requirements, whether its rendering matches the markup you need, and whether your host permits any external rendering dependencies. For mPDF, consult the repository for version-specific compatibility and extensions, including mbstring and gd; additional extensions may be needed for advanced features.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Handle external resources and untrusted HTML safely

Remote images and stylesheets

Dompdf disables remote access by default. Its documentation says remote files require enabling isRemoteEnabled and having cURL or allow_url_fopen; local and web resource references are also restricted for security. Only enable remote loading if the document requires it, and limit the files and hosts the renderer is allowed to access. Do not let an untrusted user choose arbitrary resource URLs for the server to fetch.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.

User-provided markup

Treat HTML, CSS, and resource references supplied by users as untrusted input. mPDF explicitly warns against passing outside HTML/CSS directly to its renderer and calls for vetting and sanitization beyond ordinary browser-level sanitization. Apply an appropriate validation and sanitization policy before rendering with any library; a renderer’s ability to accept HTML does not make arbitrary markup safe.

Troubleshoot common failures

  • Class not found: Confirm composer require dompdf/dompdf completed, the application includes the correct vendor/autoload.php, and the deployed files include Composer dependencies.
  • Missing extension or dependency errors: Compare the server’s PHP version and enabled extensions with the requirements for the precise library release, then install the missing dependency or choose a compatible release. Dompdf’s repository lists DOM, php-font-lib, and php-svg-lib, with MBString and GD relevant to processing support.
  • Remote image or stylesheet is absent: Remote fetching is disabled by default in Dompdf. If it is genuinely required, configure isRemoteEnabled, ensure cURL or allow_url_fopen is available, and check the resource restrictions and URL accessibility.
  • Layout differs from the browser: The renderer implements bounded HTML/CSS support rather than guaranteeing browser equivalence. Simplify unsupported layout features, use print-focused CSS, and check the selected engine’s documentation for the behavior your document depends on.
  • PDF download is corrupt or shows PHP output: Check for warnings, debug output, or whitespace emitted before the PDF response. Keep the response dedicated to the PDF, or use output() and handle the returned bytes directly.
  • PDF cannot be saved: Check that the target directory exists and is writable by the PHP process, and handle filesystem errors rather than assuming the write succeeded.

Or skip the browser setup

If the task is to capture a rendered web page as a PDF rather than generate a PDF inside your PHP application, ScreenshotNeo offers a screenshot API and MCP server. A request can return a PDF; it is a different approach from a PHP renderer, so use the PHP method above when your application needs to construct its own document.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.pdf

See the ScreenshotNeo API documentation for the PDF request options and account setup. ScreenshotNeo removes cookie banners, newsletter popups, and chat widgets before capture; bot checks, blank pages, and failed loads are never billed. Its MCP server lets AI agents take screenshots. The free plan includes 1,000 screenshots a month with no card, and paid plans start at $5 for 3,000. Sign up for free.

Frequently Asked Questions

Can I use Dompdf to convert a complete HTML file?

Yes. Read or generate the file’s HTML in PHP and pass the markup to loadHtml(); ensure any referenced resources are accessible under the renderer’s configuration and restrictions.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Does Dompdf render every CSS feature supported by Chrome?

No. Its documented support is mostly CSS 2.1 with selected CSS3 behavior, not a promise of full modern browser compatibility.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.