Converting PDFs to PNGs with Dragonfly - ruby-on-rails

I have a Dragonfly processor which should take a given PDF and return a PNG of the first page of the document.
When I run this processor via the console, I get back the PNG as expected, however, when in the context of Rails, I'm getting it as a PDF.
My code is roughly similar to this:
def to_pdf_thumbnail(temp_object)
tempfile = new_tempfile('png')
args = "'#{temp_object.path}[0]' '#{tempfile.path}'"
full_command = "convert #{args}"
result = `#{full_command}`
tempfile
end
def new_tempfile(ext=nil)
tempfile = ext ? Tempfile.new(['dragonfly', ".#{ext}"]) : Tempfile.new('dragonfly')
tempfile.binmode
tempfile.close
tempfile
end
Now, tempfile is definitely creating a .png file, but the convert is generating a PDF (when run from within Rails 3).
Any ideas as to what the issue might be here? Is something getting confused about the content type?
I should add that both this and a standard conversion (asset.png.url) both yield a PDF with the PDF content as a small block in the middle of the (A4) image.

An approach I’m using for this is to generate the thumbnail PNG on the fly via the thumb method from Dragonfly’s ImageMagick plugin:
<%= image_tag rails_model.file.thumb('100x100#', format: 'png', frame: 0).url %>
So long as Ghostscript is installed, ImageMagick/Dragonfly will honour the format / frame (i.e. page of the PDF) settings. If file is an image rather than a PDF, it will be converted to a PNG, and the frame number ignored (unless it’s a GIF).

Try this
def to_pdf_thumbnail(temp_object)
ret = ''
tempfile = new_tempfile('png')
system("convert",tmp_object.path[0],tmpfile.path)
tempfile.open {|f| ret = f.read }
ret
end
The problem is you are likely handing convert ONE argument not two

Doesn't convert rely on the extension to determine the type? Are you sure the tempfiles have the proper extensions?

Related

Rails ActiveStorage: Attach a Vips Image

I'm struggling to attach a Vips:Image object to an ActiveStorage object.
I use Vips to compress a PNG image. I'm then looking to save this compressed PNG version into a second ActiveStorage attachment. The code is failing when attempting to attach. All I get is: Process finished with exit code -1073741819 (0xC0000005)
# compress the image and save the compressed version of the file as a PNG
# both img and img_to_compress are active storage attachments
def compress_charlie(img, img_to_compress)
# load the image as a Vips Image object
vips_img = Vips::Image.new_from_buffer (URI.open(img.url) { |f| f.read }), ''
# do compression, etc ... not bothering to show this code as it has no impact on the issue I have
# save the compressed png
img_to_compress.attach(
io: StringIO.new(vips_img .write_to_buffer('.png')),
filename: img.filename
)
end
Any help is appreciated, Charlie
Ruby 3.1
Rails 7.0.1
You're decompressing and recompressing twice. How about (untested):
def convert_to_png(img, img_to_compress)
# load the image as a Vips Image object
vips_img = Vips::Image.new_from_buffer (URI.open(img.url) { |f| f.read }), ''
# save as a PNG string
png = StringIO.new(vips_img.write_to_buffer('.png')),
img_to_compress.attach(io: png, filename: img.filename)
end
pngsave_buffer gives you a String with binary encoding, you don't need to save a second time. Try this test program:
require "vips"
x = Vips::Image.new_from_file ARGV[0]
y = x.pngsave_buffer
puts "y.class = #{y.class}"
I see:
$ ./pngsave.rb ~/pics/k2.jpg
y.class = String
If the image is already a PNG you'll be wasting a lot of time, of course. You could add something to detect the format and skip the conversion.

Convert a pdf to png using mini_magick in Ruby on Rails

Background
I retrieved a pdf in binary form from an API call:
base_64_binary_data = #response_label[:generate_label_response][:response_shipments][:response_shipment][:labels][:label][:content]
#pdf_file = File.open('label.pdf', 'wb') do |file|
content = Base64.decode64 base_64_binary_data
file << content
end
The above code results in a file that I can look up and is the crystal clear image I need.
Issue
I need to place this image inside a view in order to function as a label on an invoice.
The following seems to be leading to a fine solution:
#pdf = MiniMagick::Image.new(#pdf_file.path)
#pdf.pages.first.write("preview.png")
It fails on the second line.
MiniMagick::Error
`identify label.pdf` failed with error:
...2nd line...
I'd like to work to something like this functioning:
pdf = MiniMagick::Image.open "doc.pdf"
MiniMagick::Tool::Convert.new do |convert|
convert.background "white"
convert.flatten
convert.density 150
convert.quality 100
convert << pdf.pages.first.path
convert << "png8:preview.png"
end

Rails: Received string from web service should be converted to a pdf image to be displayed

When using a web service I receive a string that looks like this:
JVBERi0xLjIgDQoxIDAgb2JqDQo8PA0KL1R5cGUgL0NhdGFsb2cNCi9QYWdlcyAzIDAgUg0KL1BhZ2VNb2RlIC9Vc2VOb25lDQovUGFnZUxheW91dCAvU2luZ2xlUGFnZQ0KPj4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c3RhcnR4cmVmDQo2ODg4DQolJUVPRg0K
Sorry for that.
Anyway, this should be a pdf because that was one of the request parameters. (I could also have chosen for .gif, or .jpg in different qualities.)
I tried reading the content, like this:
#label_binary = #response_label[:generate_label_response][:response_shipments][:response_shipment][:labels][:label][:content]
binary_data = Base64.decode64(#label_binary)
#label = File.open('file_name.pdf', 'wb') {|f| f.write(Base64.decode64(binary_data))}
But what this prints then as #label is just a number (three of four digits). Same when I use urlsafe_decode64.
Not very helpful.
Preferred end-result would be a pdf image file that I can immediately display (in this case a barcode on top of an invoice for sending a package).
Could I for this purpose save the file temporarily? Meaning: every time I need to display I rebuild this image. I think that would be best.
EDIT
I am making the call now directly to a 600dpi .jpg image and then:
base_64_binary_data = #response_label[:generate_label_response][:response_shipments][:response_shipment][:labels][:label][:content]
#jpg_file = File.open('label.jpg', 'wb') do |file|
content = Base64.decode64 base_64_binary_data
file << content
end
This file shows up at my_apps_name/label.jpg.
In my filesystem I can see the picture crystal clear.
In my view I then put:
<%= image_tag #jpg_file.path %>
When I inspect my html in the browser I see:
<img src="/images/label.jpg" alt="Label">
I suppose this is easily fixed, suggestions are still welcome of course, but...
..more importantly: What if this call is made several times at once. Will rails manage? Should I maybe save these images (to my S3) or build some kind of chronejob to manage it?
(I am not sure of the added value of mini_magic anymore, since I can see the .jpg being already there.)
Then on a complete side note, maybe better left for a separate question: what if the API on the other side fails?
Step one
decode http response
require "base64"
BASE_64_BINARY_DATA = "PASTE YOUR BIG STRING HERE"
pdf_file = File.open('your_pdf.pdf', 'wb') do |file|
content = Base64.decode64 BASE_64_BINARY_DATA
file << content
end
Step two
convert pdf file to an image
add to gemfile 'gem 'mini_magick' and use 'bundle install' command
put somewhere in your code
require 'mini_magick'
pdf = MiniMagick::Image.new(pdf_file.path)
pdf.pages.first.write("preview.png")
image saved as preview.png
To display pdf image put in the view
<%= image_tag ("path_to_image/preview.jpg")%>

Convert base64 pdf string into an image

I have tried the approach in
this example and the duplicate
I still continue to receive a blank image when I render it. I need to generate a png file from this base64 string. The label_string is the base64 encoded pdf that I receive from a shipping label creater.
file = Tempfile.new(['order', '.png'])
File.open(file.path.to_s, 'wb') do |f|
f.write(Base64.decode64(label_string))
end
pdf = Services::Invoices::Pdf.new(invoice).process(file.path)
file.unlink
pdf
here is my label_string
"JVBERi0xLjQKJeLjz9MKMiAwIG9iago8PC9MZW5ndGggNjEvRmlsdGVyL0ZsYXRlRGVjb2RlPj5zdHJlYW0KeJwr5HIK4TI2UzC2NFMISeFyDeEK5CpUMFQwAEJDBV1jIz1LIGlsqGekkJyroB+RZqjgkq8QyAUARuULswplbmRzdHJlYW0KZW5kb2JqCjQgMCBvYmoKPDwvVHlwZS9QYWdlL01lZGlhQm94WzAgMCAyODggNDMyXS9SZXNvdXJjZXM8PC9Qcm9jU2V0IFsvUERGIC9UZXh0IC9JbWFnZUIgL0ltYWdlQyAvSW1hZ2VJXS9YT2JqZWN0PDwvWGYxIDEgMCBSPj4+Pi9Db250ZW50cyAyIDAgUi9QYXJlbnQgMyAwIFI+PgplbmRvYmoKMSAwIG9iago8PC9MZW5ndGggMjQ4Mi9GaWx0ZXJbL0FTQ0lJODVEZWNvZGUvRmxhdGVEZWNvZGVdL1Jlc291cmNlczw8L1Byb2NTZXQgNSAwIFIvRm9udDw8L0YxIDYgMCBSL0YyIDcgMCBSL0YzIDggMCBSL0Y0IDkgMCBSL0Y1IDEwIDAgUi9GNiAxMSAwIFIvRjcgMTIgMCBSL0Y4IDEzIDAgUi9GOSAxNCAwIFIvRjEwIDE1IDAgUi9GMTEgMTYgMCBSL0YxMiAxNyAwIFI+Pi9YT2JqZWN0PDwvRmVkRXhHcm91bmQgMTggMCBSL0dyb3VuZEcgMTkgMCBSL2JhcmNvZGUwIDIwIDAgUj4+Pj4vVHlwZS9YT2JqZWN0L1N1YnR5cGUvRm9ybS9CQm94WzAgMCA2MTIgNzkyXS9NYXRyaXhbMSAwIDAgMSAwIDBdL0Zvcm1UeXBlIDE+PnN0cmVhbQpHYXQ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MDAwMzM5MyAwMDAwMCBuIAowMDAwMDAzNDkwIDAwMDAwIG4gCjAwMDAwMDM1OTEgMDAwMDAgbiAKMDAwMDAwMzY3OSAwMDAwMCBuIAowMDAwMDAzNzcyIDAwMDAwIG4gCjAwMDAwMDM4NjggMDAwMDAgbiAKMDAwMDAwMzk2OCAwMDAwMCBuIAowMDAwMDA0MDYwIDAwMDAwIG4gCjAwMDAwMDQxNTEgMDAwMDAgbiAKMDAwMDAwNDI0NCAwMDAwMCBuIAowMDAwMDA0MzQxIDAwMDAwIG4gCjAwMDAwMDQ5NjMgMDAwMDAgbiAKMDAwMDAwNTMzMCAwMDAwMCBuIAowMDAwMDA2NjgwIDAwMDAwIG4gCjAwMDAwMDY3MjYgMDAwMDAgbiAKdHJhaWxlcgo8PC9TaXplIDIzL1Jvb3QgMjEgMCBSL0luZm8gMjIgMCBSL0lEIFs8YzIwNmM1YTk5MmMwYzk5NzIxNmE5NDQ2ZTM2MDVmOGM+PDI2MTU5NWIxZjhiMjA5MzcyZDBiMzM0M2YyMWNiZDQwPl0+PgpzdGFydHhyZWYKNjg2MgolJUVPRgo="
UPDATE: I found a similar problem at this question
In Imagemagick, you can do that with the INLINE: input structure. See http://www.imagemagick.org/Usage/files/#inline. For example:
convert 'inline:data:image/png;base64,
R0lGODlhIAAgAPIEAAAAAB6Q/76+vvXes////wAAAAAAAAAAACH5BAEAAAUALAAA
AAAgACAAAAOBWLrc/jDKCYG1NBcwegeaxHkeGD4j+Z1OWl4Yu6mAYAu1ebpwL/OE
YCDA0YWAQuJqRwsSeEyaRTUwTlxUqjUymmZpmeI3u62Mv+XWmUzBrpeit7YtB1/r
pTAefv942UcXVX9+MjNVfheGCl18i4ddjwwpPjEslFKDUWeRGj2fnw0JADs=
' b64_noseguy.png
file1 = Tempfile.new(['order', '.pdf'])
File.open(file1.path.to_s, 'wb') do |f|
f.write(Base64.decode64(label_string))
end
file = Tempfile.new(['order', '.png'])
im = Magick::Image.from_blob(open(file1.path.to_s).read).first
im.write(file.path.to_s)
pdf = Services::Invoices::Pdf.new(invoice).process(file.path)
file.unlink
pdf
This works, however; it does not seem like it would be the best solution.

Ruby, RSVG and PNG streams

I'm trying to do an image conversion in a rails app from SVG to PNG. ImageMagick didn't work out for me, due to Heroku not able / wanting to upgrade IM at this time. I'm testing out some ideas of using RSVG2 / Cairo in dev but running into a roadblock.
I can easily convert and save the SVG to PNG like this:
#svg_test.rb
require 'debugger'
require 'rubygems'
require 'rsvg2'
SRC = 'test.svg'
DST = 'test.png'
svg = RSVG::Handle.new_from_file(SRC)
surface = Cairo::ImageSurface.new(Cairo::FORMAT_ARGB32, 800, 800)
context = Cairo::Context.new(surface)
context.render_rsvg_handle(svg)
surface.write_to_png(DST)
But this only lets me write PNG files out. In the app, I need to be able to generate these on the fly, then send them down to the client browser as data. And I can't figure out how to do this, or even if its supported. I know I can call surface.data to get the raw data at least, but I don't know enough about image formats to know how to get this as a PNG.
Thanks
Ah ha! I was so close and its pretty obvious in hindsight. Simply call the surface.write_to_png function with a StringIO object. This fills the string object, which you can then get the bytes for. Here's the finished svg_to_png function I wrote, along with a sample controller that calls it. Hope this helps someone else somewhere.
ImageConvert function:
def self.svg_to_png(svg)
svg = RSVG::Handle.new_from_data(svg)
surface = Cairo::ImageSurface.new(Cairo::FORMAT_ARGB32, 800, 800)
context = Cairo::Context.new(surface)
context.render_rsvg_handle(svg)
b = StringIO.new
surface.write_to_png(b)
return b.string
end
Test controller:
def svg_img
path = File.expand_path('../../../public/images/test.svg', __FILE__)
f = File.open(path, 'r')
t = ImageConvert.svg_to_png(f.read)
send_data(t , :filename => 'test.png', :type=>'image/png')
end

Resources