how to convert php page to pdf using php?

Today, We want to share with you how to convert php page to pdf using php.In this post we will show you how to create pdf file in php with example, hear for How to convert PHP file to PDF file or create pdf file from webpage Using mpdf we will give you demo and example for implement.In this post, we will learn about PHP Generate HTML File Into PDF with an example.

convert html to pdf and send email in php

install this library using composer :

$ composer require mpdf/mpdf

Free Live Chat for Any Issue

Basic Usage


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

$pdfObject = new \Mpdf\Mpdf();
$pdfObject->submitInHTMLCode('<h2>Welcome to!</h2>');

Convert PHP webpage into PDF file


require_once __DIR__ . '/vendor/autoload.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);
$pdfObject = new \Mpdf\Mpdf();

$pdfObject->CSSselectMedia='mpdf'; // assuming you used this in the document header


$pdfObject->Output('my_doc_imp.pdf','D') this will force fully this pdf to download with the given your files name.

I hope you get an idea about html to pdf using php.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.