在Wordpress中强制PDF下载

时间:2019-12-11 03:39:50

标签: php wordpress pdf

我在服务器中保存了一个pdf文件,当用户单击按钮时,我试图强制下载。 pdf已下载,但是我无法在Adobe Reader中查看pdf,因为页面用户的HTML代码位于“%pdf”之前。

View-report.php:

<form name="reports" method="post" class="reports" >    
    <table name= "report" class="report">
        <thead>
            <tr>
                <td>column heading</td>
                <td>column heading</td>  
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>cell data</td>
                <td>cell data</td>  
            </tr>
        </tbody>
    </table>

    <br><br>
    <button type="submit" class="downloadPDF_button" name="savePDF" formtarget="_blank">Save PDF</button>
</form> 

<?php
if (isset($_POST['savePDF'])) {
    $urlname = 'http://sitename.com/uploads/test/report.pdf';

    header('Content-type: application/pdf');
    header("Content-Transfer-Encoding: Binary");
    header("Content-Disposition: attachment; filename=report.pdf");
    ob_clean();
    flush();
    readfile($urlname);
    exit();
}
?>

如何解决此问题?

0 个答案:

没有答案