简体   繁体   English

从文件中获取PDF下载的优雅方式

[英]Elegant way to get PDF download from file

I got a file that when given a ?ordernummer=123456 fetches the necessary data from DB and generates a PDF file with FPDF that forces a download of the PDF. 我得到一个文件,当给定一个?ordernummer=123456 ,它会从数据库中获取必要的数据,并使用FPDF生成一个PDF文件,该文件强制下载PDF。 Works fine. 工作正常。 Now i want to call this file from another page so that when a button or link is pressed it downloads the PDF file. 现在,我想从另一个页面调用此文件,以便在按下按钮或链接时下载PDF文件。 Right now I'm using include() , it works but is not very elegant in my opinion. 现在我正在使用include() ,它可以工作,但是我认为它不是很优雅。 I've tried using file_get_contents(); 我试过使用file_get_contents(); but thats not working either. 但是那也不行。 Does anyone have a good solution? 有没有人有一个好的解决方案?

$ordernummer = "204377";

$postdata = http_build_query(
    array(
        'ordernummer' => $ordernummer
    )
);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);

$context  = stream_context_create($opts);
$result = file_get_contents('http://url.nl/pdf/generate_pakbon.php', false, $context);

What you are looking for is "Force Download" in PHP. 您正在寻找的是PHP中的“强制下载”。 This is done by setting the headers of your file correctly followed by reading the file in your PHP file. 这是通过正确设置文件的标题,然后在PHP文件中读取文件来完成的。

Forcing a download using PHP: 使用PHP强制下载:

In case of a PDF file, you'd need these headers: 如果是PDF文件,则需要以下标题:

header("Content-disposition: attachment; filename=pakbon_".intval($_GET['ordernummer']).".pdf");
header("Content-type: application/pdf");

The filename= part is the filename you are forcing upon the download. filename=部分是您在下载时强制使用的文件名。 So not the existing file. 所以不是现有文件。

After setting the headers, you can read the file using: 设置标题后,您可以使用以下方法读取文件:

readfile('http://ledisvet.nl/pdf/generate_pakbon.php?ordernummer='.intval($_GET['ordernummer']);

If you add this to a document all together and call it 'downloadpakbon.php' you can simply link to <a href="downloadpakbon.php?ordernummer=123456">download pakbon</a> inside your page and the download will be force. 如果将其全部添加到文档中并命名为“ downloadpakbon.php”,则可以直接链接到页面内的<a href="downloadpakbon.php?ordernummer=123456">download pakbon</a> ,下载将是力。 Credits go to the example explained here: http://webdesign.about.com/od/php/ht/force_download.htm 鸣谢于此处说明的示例: http : //webdesign.about.com/od/php/ht/force_download.htm

FPDF Only method: 仅限FPDF方法:

There are other ways available as well. 还有其他可用的方法。 FPDF has a method called 'Output' which you are probably using already. FPDF有一种称为“输出”的方法,您可能已经在使用它。 There are 4 possible parameters inside this method, one of the is 'D' which stands for forcing the download: http://www.fpdf.org/en/doc/output.htm 此方法内有4个可能的参数,其中一个是“ D”,代表强制下载: http : //www.fpdf.org/en/doc/output.htm

One way to do it is to add an extra parameter to generate_pakbon.php like ?download=true and then basing the final output method on this parameter: 一种实现方法是将一个额外的参数添加到generate_pakbon.php中,例如?download = true,然后将最终输出方法基于此参数:

if($_GET['download'] === true) {
  $pdf->Output('Order123.pdf', 'D');
} else {
  $pdf->Output('Order123.pdf', 'I');
}

Your link http://ledisvet.nl/pdf/generate_pakbon.php did download in my browser (Chrome). 您的链接http://ledisvet.nl/pdf/generate_pakbon.php确实在我的浏览器(Chrome)中下载了。 To ensure this isn't a browser-specific behaviour, add the following lines at the beginning of generate_pakbon.php (NB: ensure this is before any other output) 为了确保这不是特定于浏览器的行为,请在generate_pakbon.php的开头添加以下几行(注意:请确保这在任何其他输出之前)

header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=pakbon.pdf");
header("Pragma: no-cache");
header("Expires: 0");

I would then move your quoted code into this php file. 然后,我会将您引用的代码移到该php文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM