47

I have a page which uses mPDF which when you run displays a PDF in the browser, it can also be saved from here as a PDF no problem. What I would like to happen is when the page is run and generates a PDF that the page is saved as a PDF on the server.

Here is the current code:

<?php
include $_SERVER['DOCUMENT_ROOT'].'/include/seo-check/lib/WSAclient.php';
include $_SERVER['DOCUMENT_ROOT'].'/include/seo-check/lib/WSAParser.php';

$reportID= $_GET['reportid'];

$WSAclient = new WSAclient(WSA_USER_ID,WSA_API_KEY);

$result=$WSAclient->viewReport($reportID,WSA_SUBSCRIPTION_ID,'xml','EN');

unset($WSAclient);

ob_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PDF Report</title>
<!--Add your CSS here-->
</head>
<body>    
<?php  
echo WSAParser::viewReportResponse($result);
?>
</body>
</html>
<?php
$HTMLoutput = ob_get_contents();
ob_end_clean();


//Convert HTML 2 PDF by using MPDF PHP library
include $_SERVER['DOCUMENT_ROOT'].'/include/seo-check/lib/MPDF/mpdf.php';
$mpdf=new mPDF(); 

$mpdf->WriteHTML($HTMLoutput);
$mpdf->Output();
?>

Like I said this outputs the PDF fine but could someone tell me how to save as a PDF?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Paul
  • 2,465
  • 8
  • 35
  • 60

4 Answers4

117

The mPDF docs state that the first argument of Output() is the file path, second is the saving mode - you need to set it to 'F'.

$mpdf->Output('filename.pdf','F');
T.Todua
  • 53,146
  • 19
  • 236
  • 237
Sergey Eremin
  • 10,994
  • 2
  • 38
  • 44
30

Try this:

$mpdf->Output('my_filename.pdf','D'); 

because:

D - means Download
F - means File-save only

T.Todua
  • 53,146
  • 19
  • 236
  • 237
26

This can be done like this. It worked fine for me. And also set the directory permissions to 777 or 775 if not set.

ob_clean();
$mpdf->Output('directory_name/pdf_file_name.pdf', 'F');
Mukesh Joshi
  • 2,784
  • 4
  • 24
  • 34
  • 4
    This is a better answer as it specifies how to save it to a directory. `+1` – Rotimi Dec 12 '18 at 07:35
  • this worked for me , also make sure you specfiy the path of the out put file – kinsley kajiva Nov 23 '19 at 13:20
  • @kinsleykajiva. Can you explain where can we get the output path from? This is how my code looks like but the file doesnt get saved on the server ` $mpdf->WriteHTML($echomsg); $_Globals['SITE_ROOT']=realpath(dirname(__FILE__)); $path = str_replace('\\', '/', $_Globals['SITE_ROOT']); $src= "../wp-content/themes/krystal/files/filename.pdf"; $mpdf->Output($src,'F'); $msg= ""; echo $msg; ` – Far Jul 27 '20 at 10:15
  • @Far have you set the folder permission that allows your PHP to make writes to the folder if your using Linux? if that's so check if there is any data in the .temp folder. – kinsley kajiva Jul 28 '20 at 14:12
4

The Go trough this link state that the first argument of Output() is the file path, second is the saving mode - you need to set it to 'F'.

 $upload_dir = public_path(); 
             $filename = $upload_dir.'/testing7.pdf'; 
              $mpdf = new \Mpdf\Mpdf();
              //$test = $mpdf->Image($pro_image, 0, 0, 50, 50);

              $html ='<h1> Project Heading </h1>';
              $mail = ' <p> Project Heading </p> ';
              
              $mpdf->autoScriptToLang = true;
              $mpdf->autoLangToFont = true;
              $mpdf->WriteHTML($mail);

              $mpdf->Output($filename,'F'); 
              $mpdf->debug = true;

Example :

 $mpdf->Output($filename,'F');

Example #2

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('Hello World');

// Saves file on the server as 'filename.pdf'
$mpdf->Output('filename.pdf', \Mpdf\Output\Destination::FILE);
Aslam Khan
  • 382
  • 2
  • 2