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.

Read Also:  how to convert html to pdf in php?

convert html to pdf and send email in php

install this library using composer :

$ composer require mpdf/mpdf

Basic Usage

<?php

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

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

Convert PHP webpage into PDF file

<?php 

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

$url="http://www.pakainfo.com/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);
}
$pdfObject = new \Mpdf\Mpdf();
$pdfObject->SetDisplayMode('fullwidth');

$pdfObject->CSSselectMedia='mpdf'; // assuming you used this in the document header
$pdfObject->setBasePath($url);
$pdfObject->submitInHTMLCode($html);

$pdfObject->Output('my_doc_imp.pdf','D');

$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 infinityknow.com 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.