1

My requirement is that I want to download the file hoisted in some other server from client . For this I making ajax call from js to PHP to fetch file , In php code I use below code to write pdf file:

  $fileContents =  file_get_contents("filepath.pdf");
  return $fileContents;

when I check the success Callback of ajax call , I get below information :

> %PDF-1.3
3 0 obj
<</Type /Page
/Parent 1 0 R
/Resources 2 0 R
/MediaBox [0 0 595.28 841.89]
/Contents 4 0 R>>
endobj
4 0 obj
<</Length 5494>>
stream
0.20 w
0 G
BT
/F1 12 Tf
13.799999999999999 TL
0 g
20.00 811.89 Td
....
here some more data ...
 31
0000000000 65535 f 
0000035549 00000 n 
0000036792 00000 n 
0000000009 00000 n 
0000000117 00000 n 
0000005661 00000 n 
0000005769 00000 n 
0000011156 00000 n 
0000011264 00000 n 
0000016681 00000 n 
0000016790 00000 n 
0000022174 00000 n 
0000022284 00000 n 
0000027620 00000 n 
0000027730 00000 n 
0000033220 00000 n 
0000033330 00000 n 
0000035645 00000 n 
0000035736 00000 n 
0000035832 00000 n 
0000035931 00000 n 
0000036034 00000 n 
0000036123 00000 n 
0000036217 00000 n 
0000036314 00000 n 
0000036415 00000 n 
0000036508 00000 n 
0000036600 00000 n 
0000036694 00000 n 
0000037021 00000 n 
0000037326 00000 n 
trailer
<<
/Size 31
/Root 30 0 R
/Info 29 0 R
>>
startxref
37430
%%EOF

How can I parse this data to generate a download file. I cannot directly provide the URL of pdf file to download due to security concerns , so need to fetch the file via PHP then to create pdf by using jsPDF library. Issue is in parsing this data or how can we get content of pdf from php and send it to js.

Bhupendra
  • 1,196
  • 16
  • 39

1 Answers1

0

Setting the proper PDF header at the top of your PHP code should be enough.

header('Content-Type: application/pdf');
Joose
  • 430
  • 3
  • 8
  • If you are using Content-disposition then you should set Content-type to application/octet-stream. http://stackoverflow.com/questions/20508788/do-i-need-content-type-application-octet-stream-for-file-download – Joose Feb 10 '16 at 12:06
  • I used below code ; header('Content-type: application/pdf'); header('Content-Disposition: inline; filename="' . $fileName . '"'); header('Content-Transfer-Encoding: binary'); header('Accept-Ranges: bytes'); @readfile("filepath"); But this code works when I open PHP page in browser but I need to call php from js via ajax only – Bhupendra Feb 10 '16 at 12:06
  • Then just leave the Content-Disposition header out. – Joose Feb 10 '16 at 12:08