0

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 %>
... 
Charles Smith
  • 3,201
  • 4
  • 36
  • 80

1 Answers1

0

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 }

Charles Smith
  • 3,201
  • 4
  • 36
  • 80
  • 1
    I would recommend generating each thumbnail from the original PDF, instead of using larger versions to generate smaller versions. Each resize produces a slightly blurry result, which can really add up when chaining multiple resizes. I realized that at one point and started suggesting against that in the docs. Note that ImageProcessing 1.0 started automatically sharpening thumbnails after resizing. So, if you're seeing thumbnails being noticeably blurry, I would recommend upgrading the ImageProcessing gem (though note the breaking API changes). – Janko Aug 04 '18 at 15:51