如何在PHP下载PDF?

时间:2016-11-14 12:03:29

标签: php pdf-generation

我尝试在php下载pdf,但我能做到吗?任何人都可以帮助我吗? 我尝试使用此代码,但它不起作用。

$file ="cv0.pdf"; 
$filename = "cv0descargado.pdf"; // el nombre con el que se descargara, puede ser diferente al original 
header("Content-type: application/octet-stream"); 
header("Content-Type: application/force-download"); 
header("Content-Disposition: attachment; filename=\"$filename\"\n"); readfile($file); 

2 个答案:

答案 0 :(得分:0)

$file = $_GET["file"];
if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header("Content-Type: application/force-download");
    header('Content-Disposition: attachment; filename=' . urlencode(basename($file)));
    // header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;

请尝试这种方式

答案 1 :(得分:0)

使用php和js:

<?php $path = "yourfilepath/name.pdf"; ?>

<button onclick="imageClick('<?php echo $path ?>')">Click</button>

<script>
    function imageClick(url) {
    var a = document.createElement("a");
    a.target = "_blank";
    a.href = url;
    a.click();
        }
</script>