Use PDF as an image Rails - ruby-on-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.

Related

Image path not found because of Angular routing in Rails with devise and paperclip

I am using Angular along with Rails as an API and authentication is built with devise and an avatar added with paperclip.
Every route is routed through a catch all path in routes.rb to Angular app.js with $routeProvider.
All the templates are located inside Angular, except the navigation bar which is in Rails. I can successfully view the image of the user in the navbar:
<%= image_tag current_user.avatar.url(:thumb), class: 'img-circle' %>
The :avatar is mentioned like this in the user.rb model:
has_attached_file :avatar,
:styles => { :medium => "300x300>", :thumb => "100x100#" },
:default_url => "/images/default_user_avatar.jpg"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
The problem is that despite the default image is placed inside /images/ directory, it cannot be retrieved in order to be displayed. The link in the browser If I view the page source is website.com:3000/images/default_user_avatar.jpg and it doesn't exist.
I know I am missing serious code here but this issue is complicated, so please let me know what else could I include in order to get some help.
As you should be aware, Rails asset pipeline precompiles all assets and generates new filenames for them
(i.e. assets/my-image-7e48a0e29e11ccae7aa9a0008b056933af9cb3ad347006f4bae0c5c7dab40277.png)
There are three different ways in which you can get Angular to correctly load the images.
Add your images to the public folder. This will take your assets out of the asset pipeline and you will be able to reference them by their path and name.
While this is by far the simplest solution, you should be well aware of the problems that the asset pipeline will not solve for you anymore.
For small size images, you can use their encoded values in CSS;
Use Rails asset helpers in your Angular files.
Add an .erb extension to your Angular files. It would be best if you use a single file that centralizes all assets URLs;
Use the Rails asset helper (asset_path, image_path, image_url, etc.) to get the correct asset path.
For images found inside inside app/assets/images, you need to specify only the filename, without the images/ part.
$scope.imageUrl = '<%= image_path("my-image.png") %>';

Opening pdf data as image in rails views

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

How to show image file when i select it in file browser , Carrierwave gem

I am using Carrierwave gem for image uploading. i want to achieve image upload and show after uploading completed.
i read the wiki of this gem and used the same code but not working.
= form_for #user, html: { :multipart => true, class: 'battle_form' } do |f|
= image_tag(#user.file_attachment1_url) if #user.file_attachment1?
= f.file_field :file_attachment1, class:'fileInput', name:'image1'
= f.hidden_field :file_attachment1_cache
Carrierwave Gem GITHUB
If you want to show image after upload, just use img tag and you upload file url like:
= image_tag(#image_url)
This will work after upload completed and refresh current page.
If you just want to preview this image before form submit, you may find some way with javascript to solve it, like dropzonejs may help you

Allowing User to Download Image from my S3 Bucket

I am trying to construct a method which will allow a user to download a pdf file from my s3 bucket, (which at the moment it does)
But some parts of the url may change from time to time, like the id number after /timetables, the filename (but will always be a pdf) and the id number after the filename
This is what i have so far
def download
path = "images/timetables/14/original.pdf?"
data = open("http://#{ENV['AWS_BUCKET']}.s3.amazonaws.com/#{path}/1392904333")
send_data data.read, filename: 'Timetable',
type: 'application/pdf',
disposition: 'attachment',
stream: 'true',
buffer_size: '4096'
end
is there a way to get this information on the fly or can i only hardcode it? Ideally i want to store as much as the path into variables as i can.
Could anyone show me how to do this correctly please.
Thanks
It'll be easier to use paperclip to manage the uploading. Using this you'll create a table to save a record which will have a dynamic reference to your uploaded file on S3.
The workflow goes like this:
You upload the PDF in question to your app
paperclip uploads it to S3 and saves the URL to your database
you provide a link to your users for the S3 URL of the PDF
OR you give them the link to your record and you initiate the download as you posted above
First the model which will hold the pdf file reference looks like this:
class PdfRecord < ActiveRecord::Base
has_attached_file :pdf, :storage => :s3,
:bucket => 'S3_BUCKET_NAME',
:s3_credentials => {
:access_key_id => 'AWS_ACCESS_KEY_ID',
:secret_access_key => 'AWS_SECRET_ACCESS_KEY'
}
end
Create a form for yourself to upload your pdf:
# app/views/pdf_records/new
<%= form_for #pdf_record, multipart: true do |f| %>
<%= f.label :pdf %>
<%= f.file_field :pdf %>
<%= f.submit %>
<% end %>
Using this form you can upload the pdf and paperclip will save it to S3 and keep a reference to it in your database.
Give your users a link to your pdf_record like this:
#pdf_record = PdfRecord.find(which ever one you want)
#pdf_record.pdf.url # => the S3 url
Giving your users that url will let them download the pdf directly however, if you want to hide the URL and also allow you to update the pdf without breaking the link you can give the users a link to the pdf_record and then you initiate the download from your controller:
# give your users this url, as defined in your routes.rb
pdf_record_url(#pdf_record)
Then in the show action of the pdf_record controller:
def show
pdf_record = PdfRecord.find(params[:id])
data = open(pdf_record.pdf.url)
send_data data.read, filename: pdf_record.pdf_file_name,
type: pdf_record.pdf_content_type,
disposition: 'attachment',
stream: 'true',
buffer_size: '4096'
end
Now, you can update that pdf_record whenever you want and since your users have a link to the record rather than the actual pdf file the link will always work.

Carrierwave - show thumbnail in ActiveAdmin after upload with remove/delete link

I use ActiveAdmin to upload featured images to posts. It works but after I save the post, and click on edit, the image field is empty, although the image is still attached to it.
What I want to achieve is after upload I would like to show the thumbnail of the image below the image upload field, with an option to remove/delete the image (preferably without touching the save button).
I also use the activeadmin-wysihtml5 gem along with the activeadmin-dragonfly gem which handles image uploads when I want to insert images in the body of the post through the editor.
These gems also created an Assets link in ActiveAdmin, where I can see and handle all the uploaded images. Is it possible to do this with Carrierwave? Or maybe make Carrierwave use the existing assets and upload there?
Best Wishes,
Matt
The answer to this is featured on ActiveAdmin Wiki as Showing an uploaded image in the form
f.inputs "Attachment", :multipart => true do
f.input :cover_page, :as => :file, :hint => f.object.cover_page.present? \
? f.template.image_tag(f.object.cover_page.url(:thumb))
: f.template.content_tag(:span, "no cover page yet")
f.input :cover_page_cache, :as => :hidden
end

Resources