Skip to content

Instantly share code, notes, and snippets.

@matschaffer
Created September 27, 2010 03:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save matschaffer/598566 to your computer and use it in GitHub Desktop.
Save matschaffer/598566 to your computer and use it in GitHub Desktop.
A rough translation of markdown to pdf
require 'rubygems'
require 'jekyll'
require 'maruku'
require 'pathname'
# Need pre for inline format support
gem 'prawn', '=0.11.1.pre'
require 'prawn'
require 'to_prawn'
class Converter
include Jekyll::Convertible
attr_accessor :content, :data, :ext
def initialize(filename)
file = Pathname.new(filename)
self.ext = file.extname
self.content = file.read
read_yaml(file.dirname, file.basename)
end
def transform
Maruku.new(content).to_prawn(@pdf)
#Maruku.new(content).to_html
end
def render(pdf)
@pdf = pdf
self.content = Liquid::Template.parse(self.content).render(data, {})
self.transform
end
end
file = ARGV[0]
abort "Please specify a file" unless file
Prawn::Document.generate(File.basename(file) + ".pdf") do
bounding_box([bounds.left, bounds.top - 70], :width => bounds.width, :height => bounds.height - 100) do
self.font_size = 10
font "Times-Roman"
Converter.new(file).render(self)
end
# header
repeat :all do
bounding_box [bounds.left, bounds.top], :width => bounds.width do
font "./mentone-semibol-webfont.ttf"
text "Mashion, LLC", :size => 25
font "Helvetica"
bounding_box [bounds.right - 100, bounds.top - 15], :width => bounds.width / 2 do
text "clients@mashion.net"
text "267-702-6145"
end
bounding_box [bounds.left, bounds.top - 30], :width => 400 do
text "123 Awesome Lane, Awesometown PA"
end
line_width 0.5
stroke_horizontal_rule
end
end
# footer when > 1 page
page_count.times do |i|
go_to_page(i + 1)
bounding_box [bounds.left, bounds.bottom + 25], :width => bounds.width do
font "Helvetica"
line_width 0.5
stroke_horizontal_rule
move_down(5)
text "Page #{page_number} of #{page_count}", :size => 10, :align => :center
end
end if page_count > 1
end
module MaRuKu::Out::Prawn
def to_prawn(pdf)
@pdf = pdf
array_to_prawn(@children)
end
def array_to_prawn(children)
children.each do |c|
send("to_prawn_#{c.node_type}", c)
end
end
def to_prawn_paragraph(para)
@pdf.text para.to_html, :inline_format => true
@pdf.text ' '
end
def to_prawn_entity(entity)
puts "Didn't write entity to pdf"
p entity
end
# TODO find a less hackish way to do leading-trailing margins
def to_prawn_header(header)
size = { 1 => 16, 2 => 14 }[header.level]
@pdf.text header.children.join("\n"), :size => size, :style => :bold
@pdf.text ' '
end
def to_prawn_div(div)
if div.attributes[:class] == "signature"
signature_name = div.children.map.join(" ")
@pdf.instance_eval do
text ' ', :size => 30
stroke_horizontal_line 0, 200
stroke_horizontal_line 250, 300
text ' ', :size => 4
text signature_name
draw_text "Date", :at => [250, y - bounds.absolute_bottom + 5]
text ' ', :size => 20
stroke_horizontal_line 0, 200
text ' ', :size => 4
text signature_name + " (Print)"
end
else
puts "Didn't write div to pdf: #{div.inspect}"
end
end
end
MaRuKu::MDElement.send(:include, MaRuKu::Out::Prawn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment