将PHP / HTML / CSS下载到PDF文档

时间:2014-10-30 14:01:14

标签: php wordpress pdf

这里我有一些代码,它按照说和下载,但只下载一个白色的空白文件,我试图使用这一个Wordpress,必须没有插件。该脚本有效,但它没有将任何信息下载到PDF文档中,这里只是一个白色的空白页面是代码,寻找一个解决方案,将Wordpress帖子添加到文档中,这里是链接和代码,理想情况下我想要它下载执行链接的页面。

HTML链接

<a href="http://www.WEBSITE.com/wp-content/themes/the-theme/download.php?download_file=some_file.pdf">Download file</a>

的download.php

<?php

ignore_user_abort(true);

$path = ""; // change the path to fit your websites document structure
$dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\].]|[\.]{2,})", '', $_GET['download_file']); // simple file name validation
$fullPath = $path.$dl_file;

if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
    case "pdf":
    header("Content-type: application/pdf");
    header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a file download
    break;
    // add more headers for other content types here
    default;
    header("Content-type: application/octet-stream");
    header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
    break;
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
    $buffer = fread($fd, 2048);
    echo $buffer;
}
}
fclose ($fd);
exit;
?>

1 个答案:

答案 0 :(得分:0)

请勿使用上面的代码!

它不仅不会将任何内容转换为PDF,而且还允许世界将网络服务器可读的任意文件下载为纯文本。因此,如果在wp-config.php中设置,可以下载您的mysql数据库凭据或FTP密码。

说明:代码不转换任何内容。它只获取任意文件的内容,并以PDF或八位字节流内容类型输出。拿一个文本编辑器查看你下载的“PDF”。


看起来你没有设置这条线 $path = ""; // change the path to fit your websites document structure指向some_file.pdf的完整路径。