Last active
February 13, 2017 15:19
-
-
Save degzcs/48893052bff0bd3229f804ebd35faa6a to your computer and use it in GitHub Desktop.
Shows how to create a PDF version from an uploaded image.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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