繁体   English   中英

使用TCPDF保存PDF时无输出

[英]No output when saving PDF with TCPDF

我已经在服务器中安装了tcpdf。 我已检查文件是否存在。 在javascript函数中,我通过ajax调用print.php。

onclick = "savepdf();"

JavaScript功能(启动Ajax之后)是:

function savepdf(){
    var html = 'This is a test page.';
    var parameters = "content="+html;
    ajaxRequest.open("POST", 'print.php', true);
    ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            alert('Success');
        }
    };
    ajaxRequest.send(parameters);
}

这是print.php,我从其库存示例中复制了该文件。

<?php
    header("Content-Type: application/octet-stream");
    require_once('/tcpdf/tcpdf.php'); 
    $text = $_POST['content'];
    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('SOMEONE');
    $pdf->SetTitle('Report');
    $pdf->SetSubject('Report');
    $pdf->setPrintHeader(false);
    $pdf->setPrintFooter(false);
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
    $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
    $pdf->setFontSubsetting(true);
    $pdf->SetFont('dejavusans', '', 10, '', true);
    $pdf->AddPage();
    $html = $text;
    $pdf->writeHTML($html, true, false, true, false, '');
    $pdf->Output($_SERVER['DOCUMENT_ROOT'] . '/temp/output.pdf', 'F');
    $file = $_SERVER['DOCUMENT_ROOT'] . '/temp/output.pdf';
    header('Content-Disposition: attachment; filename="'.$file.'"');
    header('Content-Length: ' . filesize($file));
    header("Cache-control: private"); //use this to open files directly                     
    readfile($file);
?>

该文件已物理保存在服务器上,但未下载。 我该如何排除故障?

您不能通过AJAX请求强制用户下载文件。 您需要将浏览器重定向到生成的文件(如果不是私有的),或重定向到将其输出的php脚本。 请参阅此处的讨论: https : //stackoverflow.com/a/9970672/1800369

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM