Skip to content

Instantly share code, notes, and snippets.

@breckenedge
Created August 18, 2012 03:17
Show Gist options
  • Save breckenedge/3384119 to your computer and use it in GitHub Desktop.
Save breckenedge/3384119 to your computer and use it in GitHub Desktop.
PDF merge controller
require 'net/http'
class Pdf::ConcatenatorsController < ApplicationController
respond_to :html, :js, :json, :xml
def index
pdf_concatenators = PdfConcatenator.all
respond_with @pdf_concatenators
end
def new
pdf_concatenator = PdfConcatenator.new
respond_with @pdf_concatenator
end
def create
flash[:success] = ""
@pdf_concatenator = PdfConcatenator.new
opts = []
uuid = UUIDTools::UUID.random_create
Dir.mkdir("tmp/pdfs") unless Dir.exist?("tmp/pdfs")
Dir.mkdir("tmp/pdfs/#{uuid}")
params[:pdfs].each do |i, pdf|
response = Net::HTTP.get(URI.parse(pdf[:url]))
file_uuid = UUIDTools::UUID.random_create
path = File.join('tmp/pdfs', uuid, file_uuid)
f = File.new(path, 'wb')
f << response
f.close
opts << path
end
opts << 'cat' << 'output' << "tmp/pdfs/#{uuid}.pdf"
system("pdftk #{opts.join(" ")}")
send_data open("tmp/pdfs/#{uuid}.pdf", 'rb').read, :filename => "#{uuid}.pdf"
end
def show
pdf_concatenator = PdfConcatenator.find(params[:id])
respond_with @pdf_concatenator
end
def edit
end
def update
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment