mPDF输出没有重命名可下载文件


mPDF output not renaming downloadable file

我正在使用mpdf库,但是我认为当我下载它时更改下载文件的名称会相当简单,文档说明如下:

<?php
Example #2
// Saves file on the server as 'filename.pdf'
$mpdf=new mPDF();
$mpdf->WriteHTML('<p>Hallo World</p>');
$mpdf->Output('filename.pdf','F');
?>

D:发送到浏览器并强制下载文件名指定的文件。http://mpdf1.com/manual/index.php?tid=125& searchstring =下载% 20名

我代码:

if(empty($_GET['module_id'])){
        echo 'Please select a module on the previous page.';
    }else{
        global $link;
        $moduleid = $_GET['module_id'];
        $assignment_id = $_GET['assignment_id'];
        $name = 'name';
        $code = 'code';
        $course_title = 'course_title';
        $title = 'title';
        $datedue = 'handin';
        $assignment_number = 'number';
        $weight = 'weighting';
        $handin = 'handin';
        $handout = 'handout';
        $feedback = 'feedback';
        $brief = 'brief';
        $submission_procedure = 'sub_details';
        $additional_notes = 'add_note';

$result = mysqli_query($link, "SELECT `code` FROM `module` WHERE `module_id` = '$moduleid'");
while($row = mysqli_fetch_row($result)){
    echo $row[0];
}
$file_name = $row[0];
ob_end_clean();

$mpdf = new mPDF('en-GB','A4','','',20,15,10,25,10,10);
$courseworkReceipt = '
<html>
<head>
     //HTML output removed
</body>
</html>
';
$mpdf->WriteHTML($courseworkReceipt);
$mpdf->AddPage();
$assignment = '
<html>
<head>
    /More HTML output removed
</body>
</html>
';
$mpdf->WriteHTML($assignment);
$mpdf->Output($file_name, 'D');    //<-- problem
exit;
}

当文件下载时,它以'download.pdf'下载,而不是我的变量$file_name。

只使用"I"参数:

$mpdf->Output("My downloadable pdf.pdf", "I");

I:将文件内联发送到浏览器(http://mpdf1.com/manual/index.php?tid=125&searchstring=download%20name)

测试,使用最新版mPDF

Hop:

$mpdf->WriteHTML($assignment);
$mpdf->Output($file_name, 'D');  
rename('download.pdf', $file_name);
exit;

我不知道你是否应该用mpdf这样做,但是在$mpdf->output()调用之前添加内容处置头可能会有所帮助:

header('Content-Disposition: attachment; filename="'.$file_name.'"');

设置内容配置设置浏览器应该将文件保存为的格式。