Skip to content

Instantly share code, notes, and snippets.

@pjaspers
Last active September 12, 2015 20:40
Show Gist options
  • Save pjaspers/27bd98ce0e03d7536c66 to your computer and use it in GitHub Desktop.
Save pjaspers/27bd98ce0e03d7536c66 to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
#
# Small script to print labels from a csv file using high tech cybering
# Usage:
#
# label_maker path/to/file
#
# Dependencies:
#
# gem install prawn-labels
require "prawn/labels"
require "csv"
abort "Needs a path" unless ARGV[0]
abort "Can't find file there." unless File.exist? ARGV[0]
path = File.expand_path(ARGV[0])
out_path = "adressen.pdf"
# CSV - Comma-Separated Cvalues (CSV)
# Yet Numbers in its infinite wisdom decides to use ';' as the separator.
#
# More Numbers.app fun: it uses `"` to quote things, normal CSV's use the `'` to quote
# stuff.
all_the_things = CSV.read(path, headers: true, col_sep: ";", quote_char: '"')
# Add a custom type because we don't have Avery labels, we have labels from Herma.
include Prawn::Measurements # Adds the mm to points helper etc
Prawn::Labels.types = {
"Herma" => {
"paper_size" => "A4",
"top_margin" => mm2pt(14),
"left_margin" => mm2pt(8),
"right_margin" => mm2pt(8),
"column_gutter" => mm2pt(2),
"row_gutter" => 0,
"columns" => 3,
"rows" => 8
}}
valid_things = all_the_things.reject do |row|
row["Naam"].nil? || row["Adres"].nil? || row["Stad"].nil?
end
Prawn::Labels.generate(out_path, valid_things, type: "Herma") do |pdf, row|
pdf.font "/Users/pjaspers/Library/Fonts/Timeless.ttf", size: 13
pdf.bounding_box([pdf.bounds.left + mm2pt(3), pdf.bounds.top - mm2pt(3)], width: pdf.bounds.width - mm2pt(3)) do
pdf.text (row["Naam"] || "").strip
pdf.text (row["Adres"] || "").strip
pdf.text (row["Stad"] || "").strip
end
end
puts "I have written a PDF to #{out_path}"
`open #{out_path}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment