Skip to content

Instantly share code, notes, and snippets.

@halfbyte
Created December 21, 2014 01:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halfbyte/d3de755ce45ebbf70f5a to your computer and use it in GitHub Desktop.
Save halfbyte/d3de755ce45ebbf70f5a to your computer and use it in GitHub Desktop.
A Simple Calendar generator. Very specific for my case - Probably going to make it more configurable one day.
source "https://rubygems.org"
gem "prawn"
require 'rubygems'
require 'bundler/setup'
require 'prawn'
require 'yaml'
MONTHS = %w(Januar Februar März April Mai Juni Juli August September Oktober November Dezember)
DAYS = %w(Mo Di Mi Do Fr Sa So)
YEAR = 2015
EXTRA_MARGIN_VERTICAL = 100
EXTRA_MARGIN_HORIZONTAL = 50
COLOR_NORMAL = "000000"
COLOR_HOLIDAY = "AA0000"
POS_CAL_VERTICAL = 2.5
HEADLINE_SPACE = 50
holidays = YAML.load_file('free_days.yml')
pdf = Prawn::Document.new(page_size: 'A4', bottom_margin: EXTRA_MARGIN_VERTICAL)
pdf.font("fonts/DejaVuSerif.ttf")
pdf.stroke_color = "000000"
def color(condition)
if condition
COLOR_HOLIDAY
else
COLOR_NORMAL
end
end
12.times do |month|
pdf.bounding_box([EXTRA_MARGIN_HORIZONTAL,pdf.bounds.height / POS_CAL_VERTICAL], width: pdf.bounds.width - (EXTRA_MARGIN_HORIZONTAL * 2), height: pdf.bounds.height / POS_CAL_VERTICAL) do
pdf.fill_color = "000000"
pdf.text(MONTHS[month], align: :center, size: 24)
col_width = pdf.bounds.width / 7
row_height = (pdf.bounds.height - HEADLINE_SPACE) / 7
pdf.bounding_box([0,pdf.bounds.height - HEADLINE_SPACE], :width => pdf.bounds.width) do
DAYS.each_with_index do |day, i|
pdf.bounding_box([i * col_width, pdf.bounds.height], :width => col_width, height: row_height) do
pdf.fill_color = color(i > 4)
pdf.text(day, align: :center, valign: :center)
end
end
row = 0
day = Date.new(YEAR, month + 1, 1)
while (day.month == month+1) do
weekday = day.wday - 1
weekday = 6 if weekday < 0
if weekday == 0 && day.day > 1
row += 1
end
pdf.bounding_box([weekday * col_width, pdf.bounds.height - (row + 1) * row_height], :width => col_width, height: row_height) do
pdf.fill_color = color(weekday > 4 || holidays.include?(day))
pdf.text(day.day.to_s, align: :center, valign: :center)
end
day += 1
end
end
end
pdf.start_new_page if month < 11
end
pdf.render_file('calendar.pdf')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment