0

I am using output buffering to grab a pdf file from a network share via PHP using a proxy-handler and readfile(). I display this in a new browser tab by specifying the content/mime type. (http://stackoverflow.com/questions/10121903/php-scandir-explorer-view-to-display-network-share-files-file-via-wamp-wor)

I use jQuery / Ajax throughout the rest of the application. Is it possible to inject the raw PDF content into a div tag using ajax but get it to display correctly? Currently the required header (header("Content-type: application/pdf")) seems to be stripped by the ajax request and it is displaying jibberish.

Edit: Thankyou all for the comments and answers. I will respond in more detail and review my options shortly.

@ Murtaza - the question link above shows how I got to the raw PDF, in addition I have used $.ajax to send the request to the div tag and used an iframe which sort of worked when I rendered it into the code as src=, rather than Ajax.

@ Brad Christie / Quentin - Perhaps I could use an iFrame or Modal to display the content? I have tested pdfobject.js which is good but requires a linked file (e.g. /files/file.pdf). It sounds horrific but what if I use PHP to write a temporary file to the web root and then display? The only other thing I can think could be useful might be the jQuery PDF library if anyone has used it?

@ Ignacio - I did try this with the following code. It rendered the jibberish. I'm probably doing it wrong as I have no experience with iFrames. // Where $filename is the raw PDF data ">

James.

James Pitt
  • 451
  • 2
  • 8
  • 19

2 Answers2

1

You can't "inject the raw PDF content into a div tag"; the PDF file is not HTML and must go through a PDF reader plugin. Try making it the target of a iframe instead.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

Is it possible to inject the raw PDF content into a div tag using ajax but get it to display correctly?

Not really. You might be able to make something of pdf.js though.

Currently the required header (header("Content-type: application/pdf")) seems to be stripped by the ajax request

Whatever means you are using to insert the data is explicitly treating it as text or HTML, not as a PDF. It is your your code is ignoring the content-type.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335