How to Render a PDF in the Browser That Is Retrieve via Rails Controller

How to render a PDF in the browser that is retrieve via rails controller

Assuming the PDF is saved in memory, use the send_data to send the data stream back in the browser.

def get_invoice
@pdf = Recurly::Invoice.find(params[:number], :format => 'pdf')
send_data @pdf, filename: "#{params[:number]}.pdf", type: :pdf
end

If the file is stored somewhere (but this doesn't seem to be your case), use send_file.

How do I display a PDF in ROR (Ruby)?

In your controller:

def pdf
pdf_filename = File.join(Rails.root, "tmp/my_document.pdf")
send_file(pdf_filename, :filename => "your_document.pdf", :type => "application/pdf")
end

In config/environment/production.rb:

config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache

or

config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx

The config modification is required because it enables the web server to send the file directly from the disk, which gives a nice performance boost.

Update

If you want to display it instead of downloading it, use the :disposition option of send_file:

send_file(pdf_filename, :filename => "your_document.pdf", :disposition => 'inline', :type => "application/pdf")

If you want to display it inline, this question will be much more complete that I could ever be.

Rails pdf file display

hey try changing datatype of column in mysql from blob to longblob & following in view :

  <% elsif (attachment.content_type==('application/pdf')) %>
<td> <embed src="<%=url_for(:action => 'pdfshow', :id => attachment.id) %>" width="80%" height='500'> </td>

Prawnto won't display pdf inline

The :inline option uses the Content-Disposition HTTP header, which relies on a browser plugin to interpret the content.

This means that the results can vary depending on the browser/OS combination you're using, Linux especially doesn't seem very good at handling this.

How do I upload a photo to generate a PDF without storing the photo

You don't actually need ActiveStorage just to accept file uploads via a form:

<%= form_with(url: {action: :upload}, multipart: true) do %>
<%= file_field_tag 'picture' %>
<% end %>

<%= form_with model: @person do |f| %>
<%= f.file_field :picture %>
<% end %>

Submitting the form would give you an ActionDispatch::Http::UploadedFile instance in the parameters:

params[:picture]
params[:person][:picture]

This is really just a fancypants wrapper around a Tempfile instance and thus it will unlink itself when rails is done with the request.

Whitelisting uploads is really easy since ActionDispatch::Http::UploadedFile is one the permitted scalar types:

params.require(:person).permit(:picture)

The whole point of ActiveStorage really is not uploads - its providing a bunch of backends so that you can attach uploads to models and store them with a minimum of effort. If you're not storing the uploads its really just bloat.

PDF's are not displaying with rails 6 active storage

First of all you are using image_tag to render PDF file which is not possible. That is why it is showing broken image in the HTML.

If you really want to preview the PDF file, embed the service url of the file in the iframe. Here is how it can be done

<iframe src="<%= rails_blob_path(@document.main_image, disposition: :inline) %>" height="200" width="300"></iframe>

Let me know, if it works.



Related Topics



Leave a reply



Submit