43

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?

4

4 回答 4

109

mPDF文档指出 的第一个参数Output()是文件路径,第二个是保存模式 - 您需要将其设置为'F'.

$mpdf->Output('filename.pdf','F');
于 2012-09-24T23:24:07.407 回答
26

尝试这个:

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

因为:

D- 表示下载
F- 表示仅文件保存

于 2013-11-29T14:03:06.983 回答
25

这可以像这样完成。它对我来说很好。如果未设置,还将目录权限设置为 777 或 775。

ob_clean();
$mpdf->Output('directory_name/pdf_file_name.pdf', 'F');
于 2015-06-02T09:18:04.487 回答
1

Go trough this link 状态的第一个参数是Output()文件路径,第二个是保存模式 - 您需要将其设置为'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;

例子 :

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

示例 #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);
于 2020-10-14T12:20:03.947 回答