Ronedog
Ronedog

Reputation: 2331

Retrieving output from a url. How do I force the dowload of a PDF from a url using php

I need to retrieve our reports from the jasperserver report engine as a PDF, then I want the PDF to be forced as a download, instead of being displayed inthe browser. The problem with displaying in the browser is we don't want the report parameters to be displayed to the end users in the url.

If I enter this URL path into the browser I get a PDF document that shows in the same browser window with all the report data:

https://mysite.com:8443/jasperserver/flow.html?_flowId=viewReportFlow&reportUnit=sample_report&output=pdf;

What I would prefer to have happen is for a download dialog box to be used and for the users to download the PDF to their computer, instead of it showing in the browser.

I've tried the following php code, but can't get it to work. I get a return value of false, but nothing in the server logs that shows an error.

ob_start();
    header("Location: $src"); /* Redirect browser */
    $report_contents = ob_get_contents();
    ob_end_clean();
    var_dump($report_contents);

I'm not really sure how to go about this...anyone got any ideas?

Thanks for the help.

Upvotes: 0

Views: 702

Answers (2)

user557846
user557846

Reputation:

how about

$source=$url
header("Expires: 0");
header("Cache-Control: private");
header("Pragma: cache");

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
readfile($source);
exit();

Upvotes: 0

Petah
Petah

Reputation: 46060

You could buffer the file to the PHP server then output with force download:

header('Content-disposition: attachment; filename=huge_document.pdf');
header('Content-type: application/pdf');
readfile('https://mysite.com:8443/jasperserver/flow.html?_flowId=viewReportFlow&reportUnit=sample_report&output=pdf;');

See the notes about using readfile over an HTTP stream wrapper http://php.net/manual/en/function.readfile.php

Upvotes: 0

Related Questions