Skip to content

Instantly share code, notes, and snippets.

@trek
Created January 26, 2009 20:35
Show Gist options
  • Save trek/52958 to your computer and use it in GitHub Desktop.
Save trek/52958 to your computer and use it in GitHub Desktop.
# convert PDF to jpg
# use sips if available, ImageMagick(`convert`) with ghostscript otherwise
# convert -quality 90 /Users/trek/Desktop/postcard.pdf /Users/trek/Desktop/postcard.png
#
# postful image should be
#
# 6.25"x4.5" (450x324)
# 0.125" trim
# 300dpi
# 1875x1350
# irb
# API classes: Mail, Document, Upload, Addressee, ReturnAddress
require 'prawn'
require 'tempfile'
require 'active_resource'
module DefaultXMLOptions
def to_xml(opts={})
super({:skip_types => true}.merge(opts))
end
end
class Postful < ActiveResource::Base; include(DefaultXMLOptions); end
class Array; include(DefaultXMLOptions); end
# class Document < Postful; self.site = ''; end
# class Section < Postful; self.site = ''; end
# class Template < Postful; self.site = ''; end
# class Address < Postful; self.site = ''; end
# class ReturnAddress < Postful; self.site = ''; end
class Mail < Postful
self.site = 'http://www.postful.com/service/'
self.collection_name = 'mail'
@headers = { 'Authorization' => 'Basic ' + Base64.encode64("[email protected]:123456").delete("\r\n")}
end
class Upload < Postful
self.site = 'http://www.postful.com/service/'
self.collection_name = 'upload'
@headers = { 'Authorization' => 'Basic ' + Base64.encode64("[email protected]:123456").delete("\r\n"),
'Content-Type' => 'application/octet-stream'
}
# Override ActiveResource::Base.collection_path to exclue format extension from url as required by Postful
def self.collection_path(prefix_options = {}, query_options = nil)
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
# "#{prefix(prefix_options)}#{collection_name}.#{format.extension}#{query_string(query_options)}"
"#{prefix(prefix_options)}#{collection_name}#{query_string(query_options)}"
end
# Override ActiveResource::Base#create to include file size in headers as required by Postful
def create
returning connection.post(collection_path, File.read(self.attributes['file_path']), self.class.headers.merge({'Content-Length' => File.size(self.attributes['file_path']).to_s})) do |response|
load_attributes_from_response(response)
end
end
end
# u = Upload.new(:file_path => '/Users/trek/Pictures/Desktops/Bears_1600x1200.jpg')
# u.save
tmp = Tempfile.new(Base64.encode64(Time.now.to_s).delete("\r\n"))
# tmp = File.new '/Users/trek/Desktop/postcard.pdf'
f = Prawn::Document.generate(tmp.path, :page_layout => :landscape, :page_size => [324.00, 450.00], :left_margin => 1, :bottom_margin => 1) do
bounding_box([10,310], :width => 250, :height => 255) do
text <<-END
Pursuant to the state open records act, I request access to and copies of unaggregated digital information about the dates, times, locations, route numbers, and stops within each route for each individual bus at each stop on each route in the AATA system for calendar year 2008 and all of January 2009 where
1) the bus was noted as "standing room only"
2) the bus was noted as "full, not accepting passengers".
This release is in the public interest, and will be primarily used to inform the general public. As such, I request a fee waiver for it. If such a waiver is not granted, then I am willing to pay up to $20 for costs associated with the duplication of materials.
END
# stroke_rectangle bounds.top_left, bounds.width, bounds.height
end
end
`convert -density 300x300 -quality 90 -resize 1875x1350 #{f.path} #{f.path}.png`
u = Upload.new(:file_path => "#{f.path}.png")
u.save
m = Mail.new({:documents => [:template => {:source => 'gallery', :name => 'Postcard: Image fit front'},
:sections =>[{:name => 'Text', :text => 'hello'},
{:name => 'Image', :attachment => u.id}
]
],
:addressees => [{:name => 'John Doe', :address => '123 Main St', :city => 'anytown', :state => 'MI', :postal_code => 10000}]})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment