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/mpdfBasic Usage
<?php require_once __DIR__ . '/vendor/autoload.php'; $mpdf = new \Mpdf\Mpdf(); $mpdf->WriteHTML('<h1>Welcome to ExpertPHP.in!</h1>'); $mpdf->Output();
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.