How to convert PHP file to PDF file or create pdf file from webpage Using mpdf

How to convert PHP file to PDF file or create pdf file from webpage Using mpdf

In this PHP tutorial, I will let you know how to convert PHP file to PDF file using mpdf library.

The mpdf is one of the best library in PHP to convert text into pdf. This help to generate PDF file from UTF-8 encoded HTML.

You can install this library using composer :

$ composer require mpdf/mpdf
Basic Usage
<?php

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

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('<h1>Welcome to ExpertPHP.in!</h1>');
$mpdf->Output();
Convert PHP webpage into PDF file

In this example, I am gettig content from a website's page and convert it into the PDF format.

<?php 

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.

Phone: (+91) 8800417876
Noida, 201301
Attention Required! | Cloudflare

Sorry, you have been blocked

You are unable to access ressim.net

Why have I been blocked?

This website is using a security service to protect itself from online attacks. The action you just performed triggered the security solution. There are several actions that could trigger this block including submitting a certain word or phrase, a SQL command or malformed data.

What can I do to resolve this?

You can email the site owner to let them know you were blocked. Please include what you were doing when this page came up and the Cloudflare Ray ID found at the bottom of this page.