Opening pdf data as image in rails views - ruby-on-rails

I have used Rails 4,
views
<div>
<%=image_tag(some_method_path), class: 'image-view' %>
</div>
controller method
def some_method
#label_image = Base64_incoded image hex
send_data #label_image, :type => 'application/pdf', :disposition => 'inline'
end
The image is not opening in view but it is opening as pdf if we run the url in window.
How to display the pdf as image in the div ?
Any help appreciated

As far as I understood, what you are trying to do is to render a preview into an image tag of a PDF document. However, this is not an automatic task, and your browser won't magically perform the conversion for you.
You need a library to render the PDF and capture a preview. This is not a Rails problem, it's a generic PDF conversion problem.
Note. This question is very similar to How to have pdf viewer in ruby

Related

How to display the variant of an image with Active Storage in a JS box?

In my view, I can display my file attached to the model with Active Storage in a popup like this:
<%= image_tag #image.variant('small') %>
It's working well.
The problem is when I want to use a variant in the link:
<%= image_tag #image.variant('small') %>
The variant code used is:
file.variant(resize:size).processed.service_url
The link seems to be good, but when I click the image, the image is not opened in my JS popup as before but opened in a new browser window. This is very strange.
I shortened the link.
https://bucket.s3.eu-west-3.amazonaws.com/variants/MmsLY3rf8yR9/38a77a69d170464c472f6d36fb3fbc28b284af0cadaa533?response-content-disposition=inline%3B%20filename%3D%22chateau.jpeg%22%3B%20filename%2A%3DUTF-8%27%27chateau-lynch.jpeg&response-content-type=image%2Fjpeg&Signature=29fe7d85fe369ea2335fa8b333d4868d8c2f2c22e1efe
Is-it a "content-disposition" issue ?
Well, this is what I did:
In my Image model, I added an action and used the rails_representation_url() method from url_helpers:
include Rails.application.routes.url_helpers # need this for
include Rails.application.routes.url_helpers
def get_variant(version="high", disposition="attachment")
variant = file_variant(version)
return rails_representation_url(variant, only_path: true, disposition: disposition)
end
In my html, I can call my method with attachment disposition:
<a href="<%= #image.get_variant('high', 'attachment') %>" rel="example_group">
I can also download the image variant directly from my controller:
def download
redirect_to #image.get_variant('high', 'attachment')
end
If you want to only display the variant in the browser window, you can use 'inline':
redirect_to #image.get_variant('high', 'inline')
Not sure it's the best option, but it works.

Use PDF as an image Rails

I'm using paperclip to upload my images, in this instance i would like to use the pdf as an image and enable the user to have the option of downloading it. I upload my images to an S3 bucket.
Currently when trying to render the image it fails to load the given URL
<% for i in #timetable %>
<%= image_tag(i.photo.url(:timetable)) %>
<% end %>
Whereas the exact same code works if the image type is png or jpeg for example
The HTML generated is
<img src="http://ymcagym.s3.amazonaws.com/images/timetables/13/timetable.pdf?1392893849" alt="Timetable">
When putting the link in the url it renders but opens in a pdf reader
How would i go about getting it to open as an image? and also having the option to download
The
Thanks
You can enforce the file format when specifying your styles, for example:
has_attached_file :photo,
:styles => {
:timetable => ['100x100#', :jpg],
...
}
this should create a preview image of the first page for you on upload. Be sure to run rake paperclip:refresh to regenerate your assets.

Raills app is sending a blank pdf

In my rails app I have a STATIC pdf here "../public/camp.pdf" visitors can download it, but when they do the file size is extremely small and the pdf wont open. I think my rails app is sending a blank pdf. Any ideas?
def download_1
send_data '/camp.pdf', :type => 'application/pdf', :filename => "camp.pdf"
end
View:
<%= link_to 'PDF', home_download_1_path %>
EDIT: contents of the pdf is only this "/REGLAS_DE_CARBONO_MEXICO2.pdf"
Edit:
The answer is to use an HTML5 download attribute:

Rails prawn gem - multiple dispositions?

I am using the prawn gem in Rails to generate a PDF. Currently I am using disposition: "inline" to render the PDF in the browser (and in my view: <%= link_to "PDF", product_path(#product, format: "pdf") %>. Is there any way that I can have multiple dispositions so that in my view I could have one link that renders the PDF in the browser and a separate link that will automatically download the PDF?
You can insert link as you've specified to render the .pdf, and call to a specific action of a controller, in which you have to send the .pdf as a file, for example with #send_file method:
send_file '/path/to.pdf', type: 'document/pdf'

rails: produce pdf and render it as image

I'm trying to produce a pdf and send it as a file, or, better, render it as an image in the browser, using imagemagick and wicked_pdf.
I'm able to send something, but it's not recognized as an image by the OS.
The pdf string is generated correctly, so I suppose the problem is in the image part
this is the code of my controller:
format.jpg do
#format = :pdf
pdfstr = render_to_string 'generated_graphics/tag.erb', :pdf => "tag", :layout => nil, :page_height => '38mm', :page_width => '129mm'
file = Tempfile.new('foo')
file.write pdfstr
file.close
require 'RMagick'
pdf = Magick::ImageList.new(file.path)
send_data pdf.write("myimage.jpg"), :type => 'image/jpg'
end
Take a look at imagemagick and rmagick plugin for ruby. This allows you to do all kinds of image conversions, including PDF to jpeg.
http://rmagick.rubyforge.org/
EDIT:
untested sample code:
require 'RMagick'
pdf = Magick::ImageList.new("doc.pdf")
pdf.write("myimage.jpg")
if doc.pdf has 3 pages, this should output 3 images: myimage.jpg.0 myimage.jpg.1 myimage.jpg.2
take a look at the end of the documentation on this page, which shows
a similar example with a multi-frame gif converted to multiple PNGs
If your goal is to have an image I think you can skip the pdf stuff alltoghether, there's no sense of converting it first to pdf and then to image.
Have a look at IMGKit, it allows to generate an image from an html string.

Resources