PHP标题下载PDF

PHP标题下载PDF,php,html,pdf,Php,Html,Pdf,我已经创建了一个页面,您可以在“文本区域”中写入文本,然后当您单击“下载”时,您可以将该文件下载为.txt文件。我对其他一些扩展也做了同样的事情,效果很好。但它不适用于.PDF,我读的东西都不管用。以下是我用于.PDF下载的代码片段: <?php if($fileFormat == ".pdf"){ $content = $_POST['text']; $name = stripslashes($_POST['name']); $nameAndExt = $name.".pdf"; p

我已经创建了一个页面,您可以在“文本区域”中写入文本,然后当您单击“下载”时,您可以将该文件下载为.txt文件。我对其他一些扩展也做了同样的事情,效果很好。但它不适用于.PDF,我读的东西都不管用。以下是我用于.PDF下载的代码片段:

<?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 ');
}

?>

我建议使用像TCPDF这样的类,请参见。我用了好几次,它非常好(开源)

我认为这不适用于修改后的标题。你需要一个像这样的html2pdf转换器插件:好的,我会查的,谢谢!
// 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();