PHP mPDF 将文件另存为 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12574193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:50:28  来源:igfitidea点击:

PHP mPDF save file as PDF

phppdfsavempdf

提问by Paul

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.

我有一个使用 mPDF 的页面,当您运行时在浏览器中显示 PDF,它也可以从这里保存为 PDF 没问题。我想要发生的是当页面运行并生成一个 PDF 时,该页面在服务器上保存为 PDF。

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?

就像我说的那样输出 PDF 很好,但有人能告诉我如何保存为 PDF 吗?

回答by Sergey Eremin

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

MPDF文档状态的第一个参数Output()是文件路径,二是节能模式-你需要将其设置为'F'

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

回答by T.Todua

Try this:

尝试这个:

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

because:

因为:

D- means Download
F- means File-save only

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

回答by M.J

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

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

ob_clean();
$mpdf->Output('directory_name/pdf_file_name.pdf', 'F');