Created
October 25, 2014 14:54
-
-
Save yosriady/6edd116f7ee43703ebff to your computer and use it in GitHub Desktop.
Rails Paperclip Multi-page PDF Watermark Processor with MiniMagick
This file contains hidden or 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 Paperclip | |
| class Watermark < Processor | |
| WATERMARK_PATH = "#{Rails.root}/public/images/watermark.png" | |
| def initialize(file, options = {}, attachment = nil) | |
| super | |
| @file = file | |
| @watermark_path = options[:watermark_path] | |
| @current_format = File.extname(@file.path) | |
| @basename = File.basename(@file.path, @current_format) | |
| end | |
| def make | |
| pdf = MiniMagick::Image.open(File.expand_path(@file.path)) | |
| watermark = MiniMagick::Image.open(File.expand_path(WATERMARK_PATH)) | |
| composited = pdf.pages.inject([]) do |composited, page| | |
| composited << page.composite(watermark) do |c| | |
| c.compose "Over" | |
| c.gravity "SouthEast" | |
| end | |
| end | |
| MiniMagick::Tool::Convert.new do |b| | |
| composited.each { |page| b << page.path } | |
| b << pdf.path | |
| end | |
| return pdf | |
| end | |
| end | |
| end |
If I comment out the entire file - there is no difference. This is the only processor I am calling in my model.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My pdf gets uploaded but the watermark is not placed. Any ideas? It's a multipage pdf.