0

I am receiving a PDF file in base64 form. I want to show it in a new tab. I tried this -

This is my controller code for sending the data back to browser

send_data Base64.decode64(params[:base64_file]), :type => 'application/pdf', :filename => "#{params[:name]}.pdf", :disposition => "inline"

View file :

<a href='<%= show_pdf_url({:pdf_body => pdf_body, :name => "download.pdf" %>' target='_blank'>

But it throws 'Request entity too large' error.

Tried to change it to post request by -

<a href='#' class='pdf-download'/>

and calling -

$('.pdf-download').on('click', function(event){
var get_all_admin = jQuery(this);
var w = window.open('about:blank');
w.document.body.appendChild(w.document.createElement('iframe')).src = 'data:application/octet-stream;base64,${get_all_admin.data("base64_file")}';

but it does nothing, and end up loading the same page in new tab.

Help me out, I am stuck here.

dharmesh
  • 452
  • 4
  • 12
Darpan
  • 5,623
  • 3
  • 48
  • 80
  • This is issue with the server that handling your download request. Can you let us know what are server are you using for running rails? Do you use any reverse proxy server like Nginx/Apache? – dharmesh Apr 15 '19 at 12:14
  • Yes, I am using a reverse proxy. But I doubt that should be the case, because I have one more API call where instead of base64, I am returning file, which works well with send_data – Darpan Apr 15 '19 at 12:18
  • Can you explain more , because there is no params[:base64_file] and params[:name] in api call that is passed to controller. Also, if you are saving the uploaded base64 file, then you can try using send_file instead of send_data. – dharmesh Apr 15 '19 at 13:53
  • Oh, consider I am sending it. BTW I found one solution that works for me. Will post that here soon. – Darpan Apr 15 '19 at 13:55
  • Great.! share us the solution with cause of this problem as well. Which will help others to understand the root cause of the issue. – dharmesh Apr 15 '19 at 13:58
  • Added my solution @dharmesh – Darpan Apr 15 '19 at 14:12

1 Answers1

0

So, I ended up using this - Instead to trying to appending it to the new iFrame, I wrote the output to the iFrame. I really wished I was able to solve this using send_data -

   $(document).on('click', '.pdf-download', function(event){
    var get_all_admin = jQuery(this);
    event.preventDefault();

    let pdfWindow = window.open("")
    pdfWindow.document.write("<iframe style='position: fixed; top: 0; left: 0;' width='100%' height='100%' src='data:application/pdf;base64, " + encodeURI(get_all_admin.data("base64_file"))+"'></iframe>")
  });

Where get_all_admin gets data that I set in my url like this -

<a href='#' data-base64_file='<%=mydoc.body%>' data-name='<%= get_pdf_name(mydoc.id, mydoc.invoice_date) %>' target='_blank' class='pdf-download' >

here mydoc is the output in which I am getting the pdf file in Base64 format.

Darpan
  • 5,623
  • 3
  • 48
  • 80