0

I am using PDF.js to open a PDF file currently and its working fine.

Previously I was using like this:

http://myapp.mycompany.com/web/viewer.html?file=http://myapp.mycompany.com/my-PDF-file.pdf

My Problem:

For some reasons, I really do not want to reveal the file path of my PDF file and want to move my PDF files outside root directory. Is there any other way to provide the pdf file to the viewer.html? or is this the oly way?

NOTE: I have moved all the PDF files outside my root directory. So How can I access the PDF now?

Abhinav
  • 8,028
  • 12
  • 48
  • 89

2 Answers2

4

Yes, there is. Call some function instead (and you don't even need PDF.js):

http://www.example.com/getPDF.php?fileName=pdfname.pdf

function fileName() {
    $fileName = $_GET['fileName'];
    header("Content-type:application/pdf");
    header("Content-Disposition:inline;filename='{$fileName}'");
    readfile(__DIR__."/../pdfs/private/{$fileName}");
}
Justinas
  • 41,402
  • 5
  • 66
  • 96
  • sorry..I want to use PDF.js, since it provides a good looking reviewer ,how can I use PDF.js to load a file, do u have any idea on that? – Abhinav Mar 07 '16 at 12:53
  • 1
    Change your URL to `http://myapp.mycompany.com/web/viewer.html?file=http://myapp.mycompany.com/getPDF.php?filename=pdfname.pdf` and the above response would be the contents of file `getPDF.php` you may need to urlencode the part after `file=` (depending on the implementation in viewer.html) – apokryfos Mar 07 '16 at 12:54
  • @apokryfos: hey I got it worked,thank you so much for your suggestion :) – Abhinav Mar 08 '16 at 05:56
  • @Abhinav PDF.js is mostly for browsers that does not support native PDF preview or if you want to make some advanced actions with your PDF. – Justinas Mar 08 '16 at 07:13
  • @Justinas: yeah,I was looking for some advanced actions thats why I wanted to implement it through PDF.js, and I solved it wid your suggestion, thnx :) – Abhinav Mar 08 '16 at 09:10
0

Use rawurlencode to encode '/getP‌​DF.php?filename=pdfname.pdf' part:

"http://myapp.mycompany.com/web/viewer.html?file=".rawurlencode("/getP‌​DF.php?filename=".rawurlencode("pdfname.pdf"))
async5
  • 2,505
  • 1
  • 20
  • 27
  • See also https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions#can-i-specify-a-different-pdf-in-the-default-viewer – async5 Mar 07 '16 at 19:32