0

I want to download a pdf file with result, I have already generate the pdf using fpdf [$pdf->Output($output_file, 'F');], the pdf file generated is saved in server.

I want user to be able to download the pdf file generated from fpdf anyway. I even tried using other output (http://www.fpdf.org/en/doc/output.htm) but none of them work except for "F". $pdf->Output($output_file, 'F');(generate pdf successful using this)

I also tried to add a form which will download the existed generated file from server to browser but nothing is working . My code is as below:

<form method="post" target="_blank">
    <input type="submit" name="download" value="Download Most Recent PDF">
    <h3>Please check the content of PDF.</h3>
</form>

    $file = plugin_dir_path(__FILE__) . "../pdf/Result.pdf";
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;

}

I want to generate result(which i successfully did) and let user download the result in pdf version.(where i am stuck).

Mht
  • 3
  • 3
  • When i click on "Download Most Recent PDF" the browser open something like this,..................%PDF-1.4 3 0 obj <> endobj 4 0 obj <> stream x�3R��2�35W(�*T0P�R0T(�Y@��� z��F�@���g �z���& ɹ �!> .� �\�iK endstream endobj 1 0 obj <> endobj 5 0 obj <> /Resources 6 0 R /Filter /FlateDecode /Length 99 >> stream x-�; �0{O1v�h�I�x�Z 1��l�;0�����XE�:\c9f�!iBB�I��Te���p�|e=a����̿d�1hd��Kdc��n��g endstream endobj 6 0 obj <> /Font <> >> endobj 8 0 obj <> endobj 10 0 obj <> stream x]��n� D�|��C�-B�RE�MT��am!����/7�z�3�`X~n�[ – Mht Dec 29 '18 at 08:17
  • why don't you just give the file path in $file ? I think your file path is not ok – Antu Dec 29 '18 at 08:20
  • "C:\xampp\htdocs\xx\wp-content\plugins\Etc\pdf\pdf\result.pdf" this is path ...my result pdf is result.pdf – Mht Dec 29 '18 at 08:27
  • $file = plugin_dir_path(__FILE__) . "../pdf/Result.pdf"; for that path ..i can put like this or no? – Mht Dec 29 '18 at 08:30
  • why you use plugin_dir_path() ? – Antu Dec 29 '18 at 08:36
  • to Get the directory of the current file – Mht Dec 29 '18 at 08:37
  • are you using raw php ? or any framework ? – Antu Dec 29 '18 at 08:54
  • php............,in wordpress – Mht Dec 29 '18 at 08:56
  • I solved it...) – Mht Jan 11 '19 at 03:31

1 Answers1

0

First check your file path is correct or not.debug with that after that if your file is not download than try below code ,

    header('Pragma: public');

    header('Expires: 0');

    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');

    header('Cache-Control: private', false); 

    header('Content-Type: application/pdf');

    header('Content-Disposition: attachment; filename="'. basename($filename) . '";');

    header('Content-Transfer-Encoding: binary');

    header('Content-Length: ' . filesize($filename));

   echo file_get_contents($filename);

    exit;
  • I even tried this, but when i submit form, the page refreshes but file is not downloading – Mht Dec 29 '18 at 08:35
  • "C:\xampp\htdocs\xx\wp-content\plugins\Etc\pdf\pdf\result.pdf" – Mht Dec 29 '18 at 08:43
  • the result.pdf is the pdf that needs to be download...when i put plugin_dir_path(__FILE__) . "../pdf/Result.pdf" or "C:/xampp/htdocs/xx/wp-content/plugins/Etc/pdf/pdf/result.pdf" either way no difference ...its not downloading – Mht Dec 29 '18 at 08:44
  • first print your path with echo $file; and in if (file_exists($file)) { echo "path exist"; die;} – Krishna Panchal Dec 29 '18 at 08:44
  • ok i understand which file you want to download.Did you check the path of $file ? is it same as this "C:\xampp\htdocs\xx\wp-content\plugins\Etc\pdf\pdf\result.pdf" path? – Krishna Panchal Dec 29 '18 at 08:47
  • ya...it says path exists – Mht Dec 29 '18 at 08:53
  • ohk, so now put pdf generation code there and tell me the file is download or getting some error!!! – Krishna Panchal Dec 29 '18 at 08:58
  • pdf is already generated and is saved in "C:\xampp\htdocs\xx\wp-content\plugins\Etc\pdf\pdf\result.pdf" so i shoukd be able to download by the code above no? the one i posted in question? there is no progress.......whenever i click to download...it shows werird contents like i posted up ..and pdf doesnt download – Mht Dec 29 '18 at 09:20
  • try this file_get_contents($filename); instead of read file hope this will help you. – Krishna Panchal Dec 29 '18 at 09:28
  • Sorry i'm troubling you by asking to many questions but i'm trying to figure out your problem..did you open your generated pdf file on server??Sometimes that happens with me that my pdf file didn't generate properly so i was getting binary data. – Krishna Panchal Dec 29 '18 at 10:30
  • oh ya...The generated pdf looks fine and is exactly how i want, its just i am unable to download it to browser....I want the user to be able to download the result pdf – Mht Jan 01 '19 at 07:37