0

our team has a Java REST service that basically creates a PDF when we make a post call to it, and returns the PDF in the response body. Here's a quick look at that service.

 @POST
 @Path("/")
 @Consumes(MediaType.APPLICATION_JSON)
 @Produces(MediaType.APPLICATION_OCTET_STREAM)
 public Response generatePDF(String data) {

      File file = new File(FILE_PATH);

      ResponseBuilder response = Response.ok((Object) file);
      response.header("Content-Disposition", "attachment; filename=my_file.pdf");
      return response.build();
 }

All we want to do is call this service from our Ruby on Rails controller and download the PDF (I'm assuming it will prompt us for a download/open dialog box). I've researched many ways to do this but nothing is working. We've tried different gems, such as Prawn, PDFKit, and WickedPDF, with no luck. We don't need to generate or edit the PDF on the Rails side. We just want a way for the user to either view or download it so they can print it. Any help would be appreciated. Thanks!

32clemons
  • 11
  • 3
  • If you don't want your rails application to make a link directly to the service, and instead get the file first, and then send it to the user, this may be what you're looking for http://stackoverflow.com/questions/2571547/rails-how-to-to-download-a-file-from-a-http-and-save-it-into-database – Clark Jun 02 '15 at 14:05
  • To add a few things, tempfiles will be automatically cleared. And instead of using the `paperclip` gem they use to write to a database, you would instead use `send_data(tempfile, :disposition => 'attachment', :type => "application/pdf")` – Clark Jun 02 '15 at 14:09
  • Thanks, that's actually getting me very close. I'm using the following logic: `tempfile = Tempfile.new('new.pdf', :encoding => 'ascii-8bit') File.open(tempfile.path,'wb') do |f| f.write response.body end send_data(tempfile, :disposition => 'attachment', :type => "application/pdf")` but i'm getting an adobe reader error now that says "Adobe Reader could not open 'my_file.pdf' because it is either not a supported file type or because the file has been damaged'. It opens fine from my folder locally though. – 32clemons Jun 02 '15 at 14:37
  • Any other decoding I would need to do in the tempfile writing? – 32clemons Jun 02 '15 at 14:38
  • Ah, I think I see the problem. When I view the response body in a text editor, the first line looks like this: **"%PDF-1.3**. I think the double quote needs to be removed. However, if I evaluate the response body in my ruby IDE, it says the first byte is the percent sign... – 32clemons Jun 02 '15 at 14:41
  • Hmm, I'll look a bit more in to it. Obviously that link I showed was an image, so it may work differently than a PDF. – Clark Jun 02 '15 at 14:47
  • Maybe try `File.open(tempfile.path, 'wb')` etc... or the segmented method here http://stackoverflow.com/questions/2263540/how-do-i-download-a-binary-file-over-http – Clark Jun 02 '15 at 14:55
  • Thank you, actually your above suggestion worked, we just had to use send_file instead of send_data – 32clemons Jun 02 '15 at 22:03
  • Ah, I was thinking in context of using a gem like Wicked_PDF, where you may want to just send the pdf, not save it (as I did in one of my applications). However in your case you are saving it, so you send it as a file, not data. Glad you got things working. – Clark Jun 02 '15 at 23:42

0 Answers0