View .pdf file using Google Doc Viewer + Rails - ruby-on-rails

In my application, I want to view my .pdf file using Google Doc Viewer.
I am using Paperclip gem to upload file. In below code,
asset.uploaded_file.path will give file:///home/rick/rick/Projects/practice/sharebox/assets/137/Screenshot_from_2014-02-26_18_46_52.png
In my asset model,
has_attached_file :uploaded_file,
:url => "/assets/get/:id",
:path => "#{Rails.root}/assets/:id/:basename.:extension"
In my index.html file
#assets.each do |asset|
<iframe src="https://docs.google.com/viewer?url=http://localhost:3000/<%= asset.uploaded_file.path %>&embedded=true" width="100%" height="800" >
<p>Your browser does not support iframes.</p>
</iframe>
end
Using this above code i always get this error 'Apologies.
There is no preview available.'
If i will copy this file:///home/rick/rick/Projects/practice/sharebox/assets/137/Screenshot_from_2014-02-26_18_46_52.png and paste in new tab in browser, it will show image.
My assets is stored in the folder that is app_name/assets/id_name/filename.
So How i should pass path of my file to view using Google Viewer?
Thanks.

Related

Ruby on Rails and rubyzip: powerpoint modification corrupted on windows

I'm using ruby on rails to modify a powerpoint presentations existing template's xml files (open xml) based on data from my postgres database.
The issue I am facing is that after the file is generated and downloaded from heroku using a windows machine, microsoft powerpoint detects the file as corrupted and attempts to repair. After repairing the file generated in powerpoint, the file opens correctly.
If i download the file from a Linux machine and send the file to a windows machine, the file opens correctly without warning or attempt to repair. The powerpoint generated also opens correctly on Open office.
Technically, these are the steps that I am using to generate the file.
Open the template from the assets folder of my application
Extract the files in a tmp folder using Rubyzip gem
Open and modify the individual files using Nokogiri
Compress the file to a .pptx file
Upload/Save file using active storage
Redirect user to the file for download
Extract files method
def self.extract_files(dir_prefix)
Zip::File.open(Rails.root.join('app', "assets", "ppt", 'test_8.pptx')) do |z|
z.each do |f|
##Extract files in a directory
f_path=File.join("tmp/#{dir_prefix}_destination", f.name)
FileUtils.mkdir_p(File.dirname(f_path))
z.extract(f, f_path) unless File.exist?(f_path)
end
end
end
Opening files for manipulations using:
chartxml = File.open(Rails.root.join('tmp', tmp_extract_folder, 'ppt', 'charts', 'chart5.xml'))
##Manipulation logic here
File.write(Rails.root.join('tmp', tmp_extract_folder, 'ppt', 'charts', 'chart5.xml'), doc.to_xml)
Re-zipping files using:
zf =ZipFileGenerator.new("tmp/#{dir_prefix}_destination", "tmp/#{dir_prefix}_zipped.pptx")
The implementation of the zip file generator is the same as the one provided on Rubyzip's github repo
I'm then storing using active storage using:
ppt = Powerpoint.new(name: "#{dir_prefix}")
ppt.file.attach(
io: File.open("tmp/#{dir_prefix}_zipped.pptx"), filename: 'Synthese.pptx', content_type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
)
ppt.save
render :js => "window.location = '#{download_ppt_path(dir_prefix: dir_prefix)}'"
The user then downloads the file:
ppt = Powerpoint.where(name: dir_prefix)
redirect_to rails_blob_path(ppt.first.file, disposition: "attachment")
I tried uncommenting the code which does the manipulation of the xml files, and just unzipping and rezipping; I still face the same issue.

Rails video safari and iOS

I'm trying to embed a video in my rails app with this code
<%= video_tag('greeting.mp4', controls: true, class: 'tnit-about-video') %>
In Chrome and Firefox everything works fine, but on Safari in Mac and general on iOS the video doesn't play.
I read on SO that the video should have certain dimensions and bitrate etc. and converted it accordingly, but it was no use.
Also tried webm instead of mp4.
In general I am open to use a different free solution than the standard rails video tag, if anyone can suggest a good videoplayer.
By default, files are loaded from public/videos. To load from assets/video add the following line to your config/application.rb file. So add this line to your application.rb.
config.assets.paths << "#{Rails.root}/app/assets/videos"
Try this code
<video loop autoplay controls="true" width='100%' height='100%' src='//some_video.mp4' type='video/mp4'></video>
OR
using rails tags
<%= video_tag (["movie.mp4", "movie.ogg", "movie.webm"] :size => "320x240", :controls => true, :autobuffer => true) %>

I use 'wkhtmltopdf' to convert html to pdf,In my html,the image is displayed,however the pdf is not.What is wrong with it?

This is my gemfile:
gem 'pdfkit'
gem 'wkhtmltopdf'
This is my route.rb:
get '/pdf' => 'search#pdf'
This is my controller:
def pdf
render 'pdf',layout:false
end
This is my erb search/pdf.html.erb:
<p>test</p>
<img src="/assets/1.jpg">
When I visit 'localhost:3000/pdf', the images can be seen, but for 'localhost:3000/pdf.pdf' they are not shown.
Both of the formats have the <p> content displayed but in the html version the image can be seen whereas in the pdf version it can't. What's the problem?
Also, I just found that I can resolve it by using a remote img url. How can I use local images?
You need to use
= wicked_pdf_image_tag '1.jpg'

How to use paperclip to upload files other than images ...like pdf,docs

I am using paperclip to upload resume in my applyforjobs.Its working fine,i mean i can get browse button to upload files.But when i show the view page its not showing the pdf file that i upload,instead its showing just the name of the file. I also checked that folder which gets generated by default and that folder contains the pdfs which i uploaded during create aaction,which means upload thing is working fine.but i am not able to show that pdf.
Following is the code in my applyforjob.rb model for paperclip attachment :
has_attached_file :resume,:styles => { :small => "150x150>" }
Following is the code of applyforjobs/form.html.haml for uploading file :
= f.label :resume, "Upload resume".html_safe
= f.file_field :resume
Following is the code of applyforjobs/show.html.haml for showing file :
= image_tag #appliedusers.resume.url(:small)
but its not showing that pdf file.What am i supposed to write to upload and show files like pdf or docs?
Since paperclip is a general purpose attachment uploading gem, as stated in it's Readme, it natively supports uploading files of any kind.
I suggest that you:
remove :styles => { :small => "150x150>" } parameter from the has_attached_file
write a paperclip callback which will generate a thumbnail image from pdf file
define a method in model, say resume_thumbnail, which will be returning a path to generated file
call the following in your view
= image_tag #appliedusers.resume_thumbnail
No wonder image_tag #appliedusers.resume.url(:small) doesn't work for you: paperclip knows nothing about PDFs. paperclip blindly sees PDF file as a common file, and thus unable to do any kind of processing applicable to images only.
# , After saving the pdf you need to create a image which convert pdf to image to show on browser
I did this like -
**self.save ##save pdf in paperclip
create_preview_image ##call this method
def create_preview_image
file_name = File.join(Rails.root, 'public', self.document.url.split('?')[0]) if self.document
return unless file_name.present?
pdf = Magick::ImageList.new(file_name)
return unless pdf.present?
png_file = Tempfile.new(["#{rand(9999).to_s}",'.png'])
pdf[0].write(png_file.path) do
self.quality = 100
end
self.preview = png_file
self.save
end**

HTML5 VIDEO is not working in my rails 3 app

I am trying to display HTML5 video in my Rails 3 app in development,i am using Sqlite3 and default webserver(Webrick).I put the video file (movie.ogg) under assets (assets/movie.ogg).The video window screen shows up but there is no video on there though.I have 3 questions...Since rails app assets doesn't have sub-folder for video(as the way it has images),where do you put video files?
Does Webrick support Html5 video?Here is my code below ,what am i missing here,to make the video work?
view
<video width="320" height="240" controls="controls">
<source src="/assets/movie.mp4" type="video/mp4" />
<source src="/assets/movie.ogg" type="video/ogg" />
<source src="/assets/movie.webm" type="video/webm" />
Your browser does not support the video tag.
</video>
config/initializers/mime_types.rb
Rack::Mime::MIME_TYPES.merge!({
".ogg" => "application/ogg",
".ogx" => "application/ogg",
".ogv" => "video/ogg",
".oga" => "audio/ogg",
".mp4" => "video/mp4",
".m4v" => "video/mp4",
".mp3" => "audio/mpeg",
".m4a" => "audio/mpeg"
})
The video_tag helper builds an HTML 5 <video> tag.
By default, files are loaded from public/videos. To load from assets/video add the following line to your config/application.rb file:
config.assets.paths << "#{Rails.root}/app/assets/videos"
Tag Usage:
<%= video_tag (["movie.mp4", "movie.ogg", "movie.webm"] :size => "320x240", :controls => true, :autobuffer => true) %>
The assets pipeline is used for static assets. If you're adding video files to your app often, you should put them somewhere else (for example, public/videos or public/system/videos). If they really are static assets, try restarting your server first.
Assuming your html is correct, unless things have dramatically changed in rails 3.1 with the asset pipeline anything contained in the public folder can be served up from the web server, so the exact location of where to store videos is up to you. According to your sources above you should put your videos in public/assets then confirm the videos are being served up by accessing http://localhost:3000/assets/movie.mp4 (or any other src url for a video).
To serve videos as static assets in Rails 4, the best way is to use the video tag:
Simply create a folder in 'assets' called 'videos' and store your videos there:
app/assets/videos/mycoolvideo.mp4
Then in your views:
<%= video_tag "mycoolvideo.mp4" %>
If you need to specify size, a poster image or add controls, add (but this is HTML, not Rails):
<%= video_tag "mycoolvideo.mp4", width: "640", height: "480", poster: "mycoolvideo.jpg", controls: true %>
Note that Rails cleverly knows that the image is in the image folder, so specifying a name is enough, without adding images/ or assets/images/ before the image name.
If you want to pass in many videos (or better said, the same video in different formats), pass an array:
<%= video_tag ["mycoolvideo.mp4", "mycoolvideo.ogg", "mycoolvideo.webm"], size: "620x480", controls: true %>
Note that for sizing you can either use size: "widthxheight" ("640x360") or separately height: and width:

Resources