Search code examples
ruby-on-railsruby-on-rails-5shrine

Shrinerb producing blurry PDF file uploads


I've searched but cannot find any info on uploading pdf files with Shrine. I have it working, but the pdfs are blurry and pixelated, even when referencing the (:original) version.

initializers/shrine.rb

require 'shrine'
require 'shrine/storage/file_system'

Shrine.storages = {
    cache: Shrine::Storage::FileSystem.new('public', prefix: 'uploads/cache'),
    store: Shrine::Storage::FileSystem.new('public', prefix: 'uploads/store')
}

Shrine.plugin :activerecord
Shrine.plugin :backgrounding
Shrine.plugin :remove_attachment
Shrine.plugin :logging
Shrine.plugin :delete_raw
Shrine.plugin :upload_endpoint
Shrine.plugin :cached_attachment_data
Shrine.plugin :restore_cached_data
Shrine.plugin :determine_mime_type

image_uploader.rb

require 'image_processing/mini_magick'
class ImageUploader < Shrine
  ALLOWED_TYPES = %w[image/jpeg image/jpg image/png image/gif application/pdf]
  MAX_SIZE = 50
  include ImageProcessing::MiniMagick

  plugin :remove_attachment
  plugin :pretty_location
  plugin :processing
  plugin :versions
  plugin :validation_helpers
  plugin :store_dimensions
  plugin(:default_url) { |_| '/img/preview-not-available.jpg' }

  Attacher.validate do
    validate_max_size MAX_SIZE.megabytes, message: "is too large (max is #{MAX_SIZE} MB)"
    validate_mime_type_inclusion ALLOWED_TYPES
  end

  process(:store) do |io|
    original = io.download

    size_1500 = resize_to_limit!(original, 1500, 600)
    size_500 = resize_to_limit(size_1500, 500, 500)
    size_300 = resize_to_limit(size_500, 300, 300)

    { original: size_1500, medium: size_500, thumb: size_300 }
  end
end

products/show.html.erb

...
<% if @product.spec_sheet.present? %>
  <div class="col-md-12">
    <%#= image_tag(@product.spec_sheet_cover_url(:original), class: 'border') %>
    <%= link_to @product.spec_sheet_url(:original), :class => 'btn', style: 'width: auto' do %>
          <span><%= image_tag 'pdf-icon.png', style: 'width: 10%;
              position: relative;
              right: 3px;
              bottom: 1px;' %> Collection Brochure</span>
    <% end %>
  </div>
<% end %>
... 

Solution

  • I just figured out my own issue...was in front of me the whole time. By referencing :original, I was choosing a resized version. But by refactoring this line, it made the :original size full resolution. { original: io, large: size_1500, medium: size_500, thumb: size_300 }