doulangpeng3933 2014-02-14 16:02
浏览 36

PHP Header下载PDF

I've made a page where you can go and write text in a "textarea" and then when you click download you download that file as a .txt file. I've done the same thing to some other extensions and that is working fine. But it won't work with .PDF, nothing I read works. Here is the snippet I use for the .PDF downloading:

<?php

if($fileFormat == ".pdf"){

$content = $_POST['text'];
$name = stripslashes($_POST['name']);
$nameAndExt = $name.".pdf";
print strip_tags($content);

header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="'.$nameAndExt.'"');
header('Content-Transfer-Encoding: binary ');
}

?>

I'm grateful for any answear, thanks!

  • 写回答

2条回答 默认 最新

  • douying6206 2014-02-14 16:09
    关注
    // hold the filename for use elsewhere so you don't have to append .pdf every time
    $filename = "$id.pdf";
    
    // create the file
    $pdf->output( $filename );
    
    // set up the headers
    header("Content-Description: File Transfer");
    header("Content-disposition: attachment; filename={$filename}");
    header("Content-Type: application/pdf");
    header("Content-Transfer-Encoding: binary");
    header('Content-Length: ' . filesize($file));
    
    // push the buffer to the client and exit
    ob_clean();
    flush();
    
    // read the file and push to the output stream
    readfile( $filename );
    
    // remove the file from the filesystem
    unlink( $filename );
    exit();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图