How to Improve Laravel Performance
Guide to Increase Laravel Performance and Optimize it Since its emergence in 2011, Laravel has become a very popular option for developing business-focused applications including information
Read MoreSign Up Now and Get FREE CTO-level Consultation.
In today’s digital world, generating dynamic PDF documents is a common need—whether for invoices, reports, eBooks, or download-ready web content. One of the easiest ways to convert a webpage to a PDF file in PHP is by using the mPDF library.
In this blog post, we’ll guide you step-by-step on how to create a PDF file from a webpage using PHP and mPDF, complete with installation instructions and a real-world example.
In this PHP article, I will adviser you on how to catechumen PHP achievement to PDF book application mpdf library. The mpdf is one of the best libraries in PHP to catechumen argument into pdf. This advice to accomplish PDF book from UTF-8 encoded HTML.
$ composer require mpdf/mpdf
Basic Usage
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('
Welcome to Blazingcoders!
');
$mpdf->Output();
?>
Convert PHP web file into PDF file In this post, I am getting agreeable from a website's file and create it into the PDF architecture.
require_once __DIR__ . '/vendor/autoload.php';
$url="http://expertphp.in/index.php";
if (ini_get('allow_url_fopen')) {
$html = file_get_contents($url);
} else {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , 1 );
$html = curl_exec($ch);
curl_close($ch);
}
$mpdf = new \Mpdf\Mpdf();
$mpdf->SetDisplayMode('fullwidth');
$mpdf->CSSselectMedia='mpdf'; // assuming you used this in the document header
$mpdf->setBasePath($url);
$mpdf->WriteHTML($html);
$mpdf->Output('download.pdf','D');
$mpdf->Output('download.pdf','D')
?>
this will force this pdf to download with the given name.
To get started, install mPDF using Composer:
composer require mpdf/mpdf
If you're not using Composer, you can download mPDF manually, but Composer is recommended.
Here’s how to fetch a webpage and generate a PDF from it using mPDF:
<?php require_once __DIR__ . '/vendor/autoload.php'; use Mpdf\Mpdf; // Create an instance of mPDF $mpdf = new Mpdf(); // Fetch HTML content from the webpage $html = file_get_contents('https://example.com'); // Replace with your URL // Write HTML to PDF $mpdf->WriteHTML($html); // Output the PDF to browser $mpdf->Output('webpage.pdf', \Mpdf\Output\Destination::INLINE);
This code pulls HTML from the target webpage and displays it as a downloadable PDF in the browser.
To save the generated PDF to your server:
$mpdf->Output(__DIR__ . '/pdfs/webpage.pdf', \Mpdf\Output\Destination::FILE);
You can then serve the file for download later.
mPDF supports CSS styling including external stylesheets:
$stylesheet = file_get_contents('https://example.com/style.css'); $mpdf->WriteHTML($stylesheet, \Mpdf\HTMLParserMode::HEADER_CSS); $mpdf->WriteHTML($html, \Mpdf\HTMLParserMode::HTML_BODY);
Always use absolute URLs for external assets like images and CSS.
Instead of using a URL, you can also pass raw HTML directly:
php
$html = " <h1>Hello World</h1> <p>This is a PDF generated using mPDF in PHP.</p> "; $mpdf->WriteHTML($html); $mpdf->Output();
FAQ
1) How do I create a PDF from a webpage using PHP and mPDF?
To create a PDF from a webpage using PHP and mPDF, install the mPDF library via Composer, load the HTML content using file_get_contents()
or cURL, and pass it to WriteHTML()
method of mPDF, then output the PDF.
2) What is mPDF in PHP?
mPDF is a PHP library that converts HTML and CSS content into PDF files. It supports UTF-8, inline styles, external CSS, and is ideal for creating PDF documents from web pages or templates.
3) Can I convert an entire webpage URL to PDF using mPDF?
Yes, you can fetch the webpage content using file_get_contents('https://example.com')
or cURL, and then pass the HTML to mPDF’s WriteHTML()
function to generate the PDF.
4) How to install mPDF in PHP?
Install mPDF using Composer with this command:
composer require mpdf/mpdf
After installation, include it in your PHP script using:
php
require_once __DIR__ . '/vendor/autoload.php';
5) Can mPDF handle external CSS and images?
Yes, mPDF supports external CSS files and remote images. Make sure to use absolute URLs or properly define paths, and enable image loading with $mpdf->showImageErrors = true;
.
6) How do I save the generated PDF file on the server using mPDF?
Use this mPDF method to save a PDF file to a directory:
php
$mpdf->Output('path/to/save/filename.pdf', \Mpdf\Output\Destination::FILE);
7) Is mPDF free to use?
Yes, mPDF is an open-source library licensed under the GNU GPL v2. It is free to use in both personal and commercial projects, following the license terms.
8) How to pass dynamic HTML to mPDF in PHP?
You can pass dynamic HTML by storing it in a variable:
php
$html = "<h1>Welcome, $username</h1>"; $mpdf->WriteHTML($html);
This allows for generating personalized or data-driven PDF files.
9) Does mPDF support right-to-left (RTL) languages?
Yes, mPDF supports RTL languages like Arabic, Hebrew, and Persian. Just set the direction with CSS:
html
<body style="direction: rtl;">
10) What are the alternatives to mPDF for generating PDFs in PHP?
Some alternatives to mPDF are:
Dompdf
TCPDF
Snappy (uses wkhtmltopdf)
FPDF (basic features)
mPDF is preferred for advanced styling and better HTML/CSS support.
Request a FREE Business Plan.
Guide to Increase Laravel Performance and Optimize it Since its emergence in 2011, Laravel has become a very popular option for developing business-focused applications including information
Read MoreIn today's fast-paced digital era, mobile applications have become essential tools for businesses to connect with customers, streamline operations, and drive revenue growth. However, developing a
Read MoreIn 2025, businesses are navigating a rapidly evolving travel landscape. From streamlined processes to cost optimization, Travel Management Software (TMS) is proving to be a game-changer. Smart compani
Read More