Skip to content

Instantly share code, notes, and snippets.

@degzcs
Last active February 13, 2017 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save degzcs/48893052bff0bd3229f804ebd35faa6a to your computer and use it in GitHub Desktop.
Save degzcs/48893052bff0bd3229f804ebd35faa6a to your computer and use it in GitHub Desktop.
Shows how to create a PDF version from an uploaded image.
# encoding: utf-8
require 'carrierwave/processing/mime_types'
class DocumentUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
include CarrierWave::MimeTypes
storage :file
process :set_content_type
#
# Versions
#
version :preview do
process :convert => :jpg
process :resize_to_fit => [310, 200]
def full_filename (for_file = model.source.file)
super.chomp(File.extname(super)) + '.jpg'
end
end
version :pdf do
process :convert_to_pdf
def full_filename (for_file = model.source.file)
super.chomp(File.extname(super)) + '.pdf'
end
end
end
module CarrierWave
module MiniMagick
def convert_to_pdf
manipulate! do |img|
img.format('pdf', 0) do |convert|
convert << "-format"
convert << "pdf"
end
img
end
# HACK-NOTE: force and override the content type. This is to guarantee to upload the PDF with the correct content type.
file.content_type='application/pdf'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment