Ruby on rails 上传模糊的PDF文件

Ruby on rails 上传模糊的PDF文件,ruby-on-rails,ruby-on-rails-5,shrine,Ruby On Rails,Ruby On Rails 5,Shrine,我已经搜索过了,但是找不到任何关于使用上载pdf文件的信息。我让它工作,但PDF是模糊和像素化的,即使在引用(:original)版本时也是如此 初始值设定人/神社.rb require 'shrine' require 'shrine/storage/file_system' Shrine.storages = { cache: Shrine::Storage::FileSystem.new('public', prefix: 'uploads/cache'), store:

我已经搜索过了,但是找不到任何关于使用上载pdf文件的信息。我让它工作,但PDF是模糊和像素化的,即使在引用
(:original)
版本时也是如此

初始值设定人/神社.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
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
...
<% 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 %>
... 
image\u uploader.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
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
...
<% 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 %>
... 
需要“图像处理/mini\u magick”
类ImageUploader
products/show.html.erb

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
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
...
<% 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 %>
... 
。。。
“btn”,样式:“宽度:自动”do%>
收藏手册
... 

我刚刚解决了自己的问题……一直都在我面前。通过引用
:original
,我选择了一个调整大小的版本。但通过重构这一行,它使
:original
的大小达到了完全分辨率
{original:io,large:size_1500,medium:size_500,thumb:size_300}

我建议从原始PDF生成每个缩略图,而不是使用较大的版本生成较小的版本。每次调整大小都会产生一个稍微模糊的结果,当链接多个调整大小时,这种结果实际上会累加起来。我一度意识到这一点,并开始在文件中提出反对意见。请注意,ImageProcessing 1.0在调整大小后开始自动锐化缩略图。因此,如果您看到缩略图明显模糊,我建议升级ImageProcessing gem(尽管注意API的重大变化)。