D L
D L

Reputation: 25

Generating pdf in php using mpdf not working

I installed mpdf successfully via composer.

Here is my PHP code:

require_once __DIR__ . '/vendor/autoload.php';
$mpdf=new \Mpdf\Mpdf();
$mpdf->WriteHTML("<h1>test</h1>");
$file='abcd.pdf';
$mpdf->output($file,'I');

PDF won't generate. I tried in Chrome and Firefox. The location of autoload.php is correct. I tried D, F and S as well, as the second argument for $mpdf->output, none worked.

Upvotes: 1

Views: 4702

Answers (3)

WP Developer
WP Developer

Reputation: 1

Use this create a temporary directory and then do this

$upload_dir = wp_upload_dir();

$custom_temp_dir = $upload_dir['basedir'] . '/mpdf_temp/';

if (!file_exists($custom_temp_dir)) {
    mkdir($custom_temp_dir, 0755, true);
}
$mpdf = new \Mpdf\Mpdf(['tempDir' => $custom_temp_dir]);
$mpdf->WriteHTML("<h1>test</h1>");
$file='abcd.pdf';
$mpdf->output($file,'F');

Upvotes: 0

rejmann
rejmann

Reputation: 1

require_once __DIR__ . '/vendor/autoload.php';
$mpdf=new \Mpdf\Mpdf();
$mpdf->WriteHTML("<h1>test</h1>");
$file='abcd.pdf';

$mpdf->OutputHttpDownload($file);

You can use the OutputHttpDownload method passing the name of your file and it will work!

Upvotes: 0

Omar Tammam
Omar Tammam

Reputation: 1304

your code actually works, please double check your php version and note that mpdf is compatible only with PHP ≥ 5.6.0 and < 7.4.0.

https://mpdf.github.io/about-mpdf/requirements-v7.html

Upvotes: 1

Related Questions