0

Does anyone know how I can return a series of PDF files in a single Object from a RESTful WebService to a Javascript client?

I tried returning a PDF within a JSON object as a byte array, but that does not seem to work, or I have done something wrong with the byte array on the JS side.

pb2q
  • 54,061
  • 17
  • 135
  • 139
Ray
  • 35
  • 2
  • 5
  • I believe that the best way to do this is return a JSON object with the URL to each pdf file. Then use iframes to show the pdf. I don't know about byte encoding for PDF files. – Ohgodwhy Sep 24 '12 at 18:50
  • possible duplicate of [How to download multiple files with one HTTP request?](http://stackoverflow.com/questions/1041542/how-to-download-multiple-files-with-one-http-request) – Pointy Sep 24 '12 at 18:50

1 Answers1

0

First you need encode your binary files( your pdf files). The base 64 encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean.

I would suggest you to create a function to return one document and return a json object with some of the attributes of the file( name, extension, date,...etc.., content) and in the content field of the json object, just put the base64 string that you obtained from applying the base64 enconding function to the pdf file. In this way you can (in the client side) decode this string and get the original file without any problem.

Base64-encoded data takes about 33% more space than the original data.

Because of this I suggest you to return only one document on each call to the RESTful web service. Becasue a pdf file can be small, but it can also be huge, and combining several documents on the response json object could make your transfer very heavy. But this is up to you.

yms
  • 10,080
  • 3
  • 37
  • 66
slash28cu
  • 1,534
  • 10
  • 21
  • I tried your suggestion with just 1 PDF file, but I have not had any luck. I cannot seem to properly decode it on the client side. I tried taking the PDF on server side and encoding it as a base64 byte array (byte[]) and as a String. Any suggestions? Have you done something similar? – Ray Sep 24 '12 at 21:53
  • Yes, i did a restful web service for a company that a worked for and i used it to send a jpg file enconding it using base64. Everything worked fine. Why don't you try to take the string that you received on the client and try to decode it on a separate tool and see if at least the string you are receiving y correct and the the problem is inside function that you are using to decode the string. I can give you the real sample, but is in php and i am affraid that you are using java or .net. – slash28cu Sep 25 '12 at 19:29
  • Yes I am using Java on server side. So after some tweaks, the string I am receiving is correct, but I don't think this is possible with a PDF, only images, as per this link [link](http://stackoverflow.com/questions/5175035/set-the-source-of-adobe-pdf-browser-plugin-to-be-a-byte-stream). I have been using pdfObject.js which can take a URL well enough, but does not seem to handle a PDF in string or byte array. I think I may have to use Ohgodwhy's approach, but with pdfObject.js instead of iframes. Thanks. – Ray Sep 26 '12 at 20:44