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 post we will give you information about How to convert PHP file to PDF file or create pdf file from webpage Using mpdf. Hear we will give you detail about How to convert PHP file to PDF file or create pdf file from webpage Using mpdfAnd how to use it also give you demo for it if it is necessary.

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 MpdfMpdf();
$mpdf->WriteHTML('<h1>Welcome to onlinecode!</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://onlinecode/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 MpdfMpdf();
$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.

Try this..

Hope this code and post will helped you for implement How to convert PHP file to PDF file or create pdf file from webpage Using mpdf. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github