So I was checking on how to display PDF thumbnails in Rails since for some reason creating a thumbnail version of my file in my uploader doesn't work, and it lead me to this:
Convert a .doc or .pdf to an image and display a thumbnail in Ruby?
So I got up to this:
def show_thumbnail
require 'rmagick'
pdf = Magick::ImageList.new(self.pdf_file.file.path)
first_page = pdf.first
scaled_page = first_page.scale(300, 450)
end
But how do I display scaled_page to a webpage?
I added this function in the decorator so I can do something like this:
= image_tag(pdf.pdf_file.show_thumbnail)
But it results in this error:
Can't resolve image into URL: undefined method `to_model' for #<Magick::Image:0x0000000122c4b300>
To display the image, the browser only need a URL to the image. If you don't want to store the image on your HDD, you can encode the image into a data URL.
...
scaled_page = first_page.scale(300, 450)
# Set the image format to png so that we can quantize it to reduce its size
scaled_page.format('png')
# Quantize the image
scaled_page = scaled_page.quantize
# A blob is just a binary string
blob = scaled_page.to_blob
# Base64 encode the blob and remove all line feeds
base64 = Base64.encode64(blob).tr("\n", "")
data_url = "data:image/png;base64,#{base64}"
# You need to find a way to send the data URL to the browser,
# e.g. `<%= image_tag data_url %>`
But I highly recommend that you persist the thumbnails on your HDD or better on a CDN because such images are hard to generate but are frequently accessed by the browsers. If you decided to do so, you need a strategy to generate unique URLs to those thumbnails and a way to associate the URLs to your PDF files.
Related
Part 1: I have PDFs, Docs,Docx stored in my S3. When I download them I want them to first be converted to images (png or jpg) and then only download (as images or a thumbnail of images).
How to achieve this ?
Part 2: I have used mini-magick to convert pdf to image and its somewhat working like this:
require "mini_magick"
im=MiniMagick::Image.open("path/to_my_pdf.pdf")
im.format("png", 0)
im.write("some_thumbnail.png")
The problem here is a pdf can have multiple pages and I need each and every page to be converted into image format (may be an array of images) and I am not able to achieve it. I am only able to convert any one of the page of the pdf. Stuck here. Kindly help.
Answer any part of the question as you like. !!
You can do that using RMagick gem as following.
require 'RMagick'
pdf = Magick::ImageList.new("path/to_my_pdf.pdf")
pdf.each_with_index do |page, i|
page.write "#{i}_thumbnail.png"
end
pdf = Magick::ImageList.new(self.checklist_question.resource_file.service_url) do
self.quality = 100
end
This converts the pdf to images but does not get the point on how to save and merge them in pdf again and then upload to active storage.
You could use a different gem like carrier_wave and write the data as explained here: https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-Upload-from-a-string-in-Rails-3
I have uploaded 1 PDF then convert it to xlsx file. I have tried different ways but not getting actual output.pdf2xls only displays single line format not whole file data. I want whole PDF file data to display on xlsx file.
i have one method convert PDF to xlsx but not display proper format.
def do_excel_to_pdf
#user=User.create!(pdf: params[:pdf])
#path_in = #user.pdf.path
temp1 = #user.pdf.path
#path_out = #user.pdf.path.slice(0..#user.pdf.path.rindex(/\//))
query = "libreoffice --headless --invisible --convert-to pdf " + #path_in + " --outdir " + #path_out
system(query)
file = #path_out+#user.pdf.original_filename.slice(0..#user.pdf.original_filename.rindex('.')-1)+".pdf"
send_file file, :type=>"application/msexcel", :x_sendfile=>true
end
if any one use please help me, any gem any script.
I would start with reading from the PDF, inserting the data in the XLSX is easy, if you have problems with that ask another question and specify which gem you use and what you tried for that part.
You use libreoffice to read the PDF but according to the FAQ your PDF needs to be hybrid, perhaps that is the problem.
As an alternative you could try to use some conversion tool for ebooks like the one in Calibre but I'm afraid you will lose too much formatting to recover the data you need.
All depends on how the data in your PDF is structured, if regular text without much formatting and positioning it can be as easy as using the gem pdf-reader
I used it in the past and my data had a lot of formatting - you would be surprised to know how complicated the PDF structure is - so I had to specify for each field at which location exactly which data had to be read, not for the faint of heart.
Here a simple example.
require 'pdf/reader' # gem install pdf-reader
reader = PDF::Reader.new("my.pdf")
reader.pages.each do |page|
# puts page.text
page.page_object.each do |e|
p e.first.contents
end
end
not able to find options to convert from PDF to xsls but API Options available for converting PDF to Image and PDF to powerpoint(Link Given Below)
Not sure u can change the requirement to show results in other formats!!
http://www.convertapi.com/
I would like to display an image (PNG) generated in controller, which is not saved in a file. Is it possible to pass such generated image data to view? I did a bit of googling, but not found anything on this idea. A similar solution is provided here: Invalid encoding with rqrcode, saving image in temporary file and assigning it back, but I am trying to avoid this extra step.
I tried to use send_data disposition: 'inline' but I am stuck how to use this object in my view code.
Take a look at data URIs. That might solve what you're looking for.
The wikipedia article even has an example for PNGs.
It's possible to load an image's data inline using Data URIs and Base 64 encoding:
<img src="data:image/png;base64,/<%= #image_data_b64 %>" />
You can then use ActiveSupport::Base64.encode64 to encode the image data:
class ImageController < ApplicationController
def index
raw_data = # Code to get image data as string
#image_data_b64 = ActiveSupport::Base64.encode64(raw_data)
end
end
I'm developing a Ruby on Rails application that requires file uploading/downloading. For the upload part i used the gem carrierwave since it's very easy to use and flexible. The problem is: once i uploaded the file, i need to know a few things: i.e. if it's a pdf instead of downloading the file i show it inline,and the same goes for an image. How do i get the file extension and how can i do it to send the file to a user?? Any feedback is appreciated Thanks!!
Determine file extension (I suppose a name for mounted uploader is 'file'):
file = my_model.file.url
extension = my_model.file.file.extension.downcase
Then prepare mime and disposition vars:
disposition = 'attachment'
mime = MIME::Types.type_for(file).first.content_type
if %w{jpg png jpg gif bmp}.include?(extension) or extension == "pdf"
disposition = 'inline'
end
(add other extensions if you want).
And then send the file:
send_file file, :type => mime, :disposition => disposition
Once you have uploaded a file, the name is stored in the database. This also includes the extension. Assuming you have a User model with an uploader mounted as asset, then you can get it as:
user.asset.file.extension
As for sending it to the user, if you call user.asset_url, it will give you the URL where the file is uploaded. The user can use that link to get the file. Or am I misunderstanding what you mean by "send the file to a user"?