1

I have a webpage in wordpress.

In a page template I make a call via Ajax to a php function that returns a pdf stream made by php-pdftk library call.
The pdf stream seems to return fine to the success event of the jquery ajax call, but I can not find a way to send this stream to the browser.
It seems there is no direct way to output to a new browser window nor a object or a embed in the same document.

I have read almost every answer to this question in SO and other sites, but it seems it cannot be done.

Does anyone find a way to do this from JS or Jquery, or this is impossible from the conceptual point of view?
What would be the best way to do this?

mmm
  • 1,070
  • 1
  • 7
  • 15
  • Have the php function create a tempory file in a web accesable location, and return the url. The js success function then simply directs the browser to open the url – Steve Aug 03 '15 at 14:12
  • Thanks Steve, I assume your answer means that there is no way (or easy way) to output the pdf stream directly to browser then? – Christian Ariel Galvez Aug 03 '15 at 14:48
  • Not in my experience - i did pretty much the same research you have just done last year, and ended up using the above method. There are things like http://mozilla.github.io/pdf.js/ that could be used, but i decided the effort to implement it was unjustifiable. There are SO questions that might help if you still want to try that route: http://stackoverflow.com/questions/24535799/pdf-js-and-viewer-js-pass-a-stream-or-blob-to-the-viewer – Steve Aug 03 '15 at 15:14
  • Just one more tought, when you would erase that pdf from server? A cron on the server that makes a "garbage collect" all the pdf files? Or how? – Christian Ariel Galvez Aug 05 '15 at 14:25
  • Yes, a cron job is what i used. Alternatively you can ditch the ajax all together, and instead create a hidden form that submits the data that you want, so the returned pdf data stream is handled directly by the browser (resulting in a download, or open if browser has pdf ability) – Steve Aug 05 '15 at 14:39
  • If you don't want to generate a file then have a cron job clean it up, I would highly recommend DomPDF. It can stream it directly to the browser. – Chad Reitsma May 04 '22 at 16:45

0 Answers0