How to embed an image from Active Storage in Prawn PDF? - ruby-on-rails

I am trying to embed an image from Rails' Active Storage into a Prawn PDF.
This is what I've got in my Prawn PDF class:
path = #view.rails_blob_url(#logo, :host => "localhost:3000", :protocol => "http", :locale => nil)
image(path, :vposition => :center)
When I try to open the PDF, I get this error:
ArgumentError in InvoicesController#show
http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBGdz09IiwiZXhwIjxudWxsHCJwdXIiOiJibG9iX2lkIn19--06167c36c283f6d5de63ae306b721310af11f70e/Test-Logo.png
not found
When I copy that exact same URL into my browser, the image shows up as expected. 5 minutes later it expires (?).
What am I missing here?
How can I show the image in my PDF?
Why can the image be rendered in the browser but not in my PDF?
I've spent all day trying to get this to work today, but to no avail.

This is what worked for me:
def initialize_globals
logo_from_object = #organization.logo
#logo = StringIO.open(logo_from_object.download)
..
..
..
end
def print_header
image #logo, scale: 0.10
end

Give this a shot:
image ActiveStorage::Blob.service.send(:path_for, #company.logo_image.key), at: [X, Y], width: DESIRED_WIDTH
Where logo_image is the actual ActiveStorage::Attached::One object.
E.g
class Company
has_one_attached :logo_image
end
Also, I believe as of now, only 2 types are supported: png and jpg.

Related

Rails 4, Paperclip on Heroku does not recognize broken image

having trouble solving problem with pictures on my project.
Summary: Rilas 4 hosted on Heroku using Paerclip with S3
Problem starts with having to use previously used custom uploading logic with S3. picture url looks something like this /profile_picture/:style_:image_hash. It works fine with images that are there but with images that are not present paperclip still trying to access image that is not there and the actual link look something like this: http://s3.amazonaws.com/project/profile_pictures/100h_.
has_attached_file :picture,
styles: { :'53h' => '', :'100h' => '' },
convert_options: {
:'100h' => '-gravity center -thumbnail 165x165^ -extent 165x165',
:'53h' => '-gravity center -thumbnail 45x45^ -extent 45x45'
},
path: 'profile_pictures/:style_:filename',
default_url: '/images/default-pp-large.jpg'
I am guessing that might be because of style inside the actual filename, but i am not sure, eather way defauly_url is not working and images all are broken, excluding the ones that are actually there.
Can you help please?
In the end i made a monkey pach to paperclip gem. Added this lines to config/initializers/paperclip.rb
module Paperclip
class Attachment
alias_method :original_url, :url
def url(style_name = default_style, options = {})
if #instance.public_send("#{#name.to_s}_file_name").blank?
'default-pp-large.jpg'
else
original_url(style_name, options)
end
end
end
end
i'm wondring how is the ID is not present in the path for the picture, as in this case if 2 differents Pictures with same name it will retrieve the first match as i think, path should be something like:
path: 'profile_pictures/:id_:style_:filename'
# OR
path: 'profile_pictures/:id/:style_:filename'
not sure if that should be totally solving the problem but that is a part of it.

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

[Ruby]IO.Read not reading the whole file

I want to download a zip file containing some images via a rails method. The problem is that when I try to read thoses files, I'm not getting the entire file. Of course, that's pretty much annoying to display the image.
Here is my code :
def download_zip(image_list)
if !image_list.blank?
file_name = "pictures.zip"
t = Tempfile.new("lolbite11")
Zip::OutputStream.open(t.path) do |z|
image_list.each do |img|
title = img.name
title += ".jpg" unless title.end_with?(".jpg")
z.put_next_entry(title)
z.print IO.read(Rails.root.join('public', img.path)) <- Here is the problem ?
end
end
send_file t.path, :type => 'application/zip',
:disposition => 'attachment',
:filename => file_name
t.close
end
end
After I've download the zip and extract it. I open my images with a text editor and sadly see that I've not the whole image... I tried to output the 'IO.read' and it's only displaying what is in my final file.
I finally found an other way to solve my problem with File.copy_stream().
But if someone knows why this doesn't work, feel free to explain to me.

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.

When using redirect_to files on s3. How to either display an image or download a file

In Rails 3 AttachmentsController, I have the following:
def show
attachment = Attachment.find_by_id(params[:id])
redirect_to(attachment.authenticated_url())
end
Where authenticated_url is simply a URL to S3 to access the file.
The problem is that the file is always downloaded by the browser. What I would like to have happen is if the file is an image/pdf, something the browser can render, show the file in the browser and only download non-browser friendly files.
Have you seen this before? Any ideas on where to start?
Thanks
send_file can be used for remote url as well
file = open("http://cdn2.example.com/somefile.pdf")
send_file(file, :filename => "example.pdf", :type => "application/pdf" , :disposition => "attachment")
Here example.pdf will be downloaded. If you want open pdf in browser itself use this
file = open("http://cdn2.example.com/somefile.pdf")
send_file(file, :filename => "example.pdf", :type => "application/pdf" , :disposition => "inline")
redirect_to #attachment.url
I'm using Paperclip as well, and this display pictures inside the browser. Do you have to do something different than this?
I think you'll want to look into the rails send_file method.
1) http://apidock.com/rails/ActionController/DataStreaming/send_file
The :disposition option lets you decide whether a file will be downloaded or displayed inline. I used this in my rails app to let users download mp3 files.
Hope this helps.

Resources