下载 PHP/HTML/CSS 到 PDF 文档


Download PHP/HTML/CSS to PDF Document

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

网页链接

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

下载.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;
?>
不要

使用上面的代码!

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

说明:代码不会转换任何内容。它只是获取任意文件的内容,并使用PDF或八位字节流内容类型输出它。使用文本编辑器并查看您下载的"PDF"。


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