Class: Courseware::Printer
- Inherits:
-
Object
- Object
- Courseware::Printer
- Defined in:
- lib/courseware/printer.rb
Instance Method Summary collapse
-
#can_print? ⇒ Boolean
Ensure that the printing toolchain is in place.
- #exercises ⇒ Object
-
#generate_html(subject) ⇒ Object
clean out the static dir and build the source html.
- #generate_pdf(subject) ⇒ Object
- #guide ⇒ Object
- #handouts ⇒ Object
-
#initialize(config, opts) ⇒ Printer
constructor
A new instance of Printer.
- #print ⇒ Object
- #solutions ⇒ Object
Constructor Details
#initialize(config, opts) ⇒ Printer
Returns a new instance of Printer.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/courseware/printer.rb', line 4 def initialize(config, opts) @config = config @course = opts[:course] or raise 'Course is a required option' @prefix = opts[:prefix] or raise 'Prefix is a required option' @version = opts[:version] or raise 'Version is a required option' raise unless can_print? @varfile = config[:presfile] or raise 'Presentation file is not set properly!' @variant = File.basename(@varfile, '.json') unless @varfile == 'showoff.json' @pdfopts = "--pdf-title '#{@course}' --pdf-author '#{@config[:pdf][:author]}' --pdf-subject '#{@config[:pdf][:subject]}'" @pdfopts << " --disallow-modify" if @config[:pdf][:protected] if @config[:pdf][:watermark] showoff = Courseware.parse_showoff(@config[:presfile]) @event_id = showoff['event_id'] || Courseware.question('Enter the Event ID:') @password = showoff['key'] || Courseware.question('Enter desired password:', (@event_id[/-?(\w*)$/, 1] rescue nil)) if @config[:nocache] @watermark_style = File.join('_support', 'watermark.css') @watermark_pdf = File.join('_support', 'watermark.pdf') else @watermark_style = File.join(@config[:cachedir], 'templates', 'watermark.css') @watermark_pdf = File.join(@config[:cachedir], 'templates', 'watermark.pdf') end end FileUtils.mkdir(config[:output]) unless File.directory?(config[:output]) if block_given? yield self FileUtils.rm_rf('static') end end |
Instance Method Details
#can_print? ⇒ Boolean
Ensure that the printing toolchain is in place.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/courseware/printer.rb', line 71 def can_print? case @config[:renderer] when :wkhtmltopdf # Fonts must be installed locally for wkhtmltopdf to find them if RUBY_PLATFORM =~ /darwin/ fontpath = File.('~/Library/Fonts') Dir.glob('_fonts/*').each do |font| destination = "#{fontpath}/#{File.basename(font)}" FileUtils.cp(font, destination) unless File.exists?(destination) end end unless system 'wkhtmltopdf --version >/dev/null 2>&1' and system 'pdftk --version >/dev/null 2>&1' msg = "We use wkhtmltopdf and pdftk to generate courseware PDF files.\n" + "\n" + "You should install them using Homebrew or directly from:\n" + " * http://wkhtmltopdf.org/downloads.htm\n" + " * https://www.pdflabs.com/tools/pdftk-server/#download" unless RUBY_PLATFORM =~ /darwin/ msg << "\n\n" msg << 'Please install all the fonts in the `_fonts` directory to your system.' end Courseware.dialog('Printing toolchain unavailable.', msg) return false end when :prince unless system 'prince --version >/dev/null 2>&1' msg = "This course is configured to use PrinceXMLto generate PDF files.\n" + "\n" + "You should install version 9 from:\n" + " * http://www.princexml.com/download/\n" + "\n" + "And the license from:\n" + " * https://confluence.puppet.com/display/EDU/Licenses" Courseware.dialog('Printing toolchain unavailable.', msg) return false end end return true end |
#exercises ⇒ Object
52 53 54 55 56 |
# File 'lib/courseware/printer.rb', line 52 def exercises $logger.info "Generating exercise guide pdf for #{@course} #{@version}..." generate_pdf(:exercises) end |
#generate_html(subject) ⇒ Object
clean out the static dir and build the source html
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/courseware/printer.rb', line 118 def generate_html(subject) case subject when :handouts, :print subject = 'print' when :exercises, :solutions subject = "supplemental #{subject}" when :guide subject = 'print guide' else raise "I don't know how to generate HTML of #{subject}." end begin # Until showoff static knows about -f, we have to schlup around files if @variant FileUtils.mv 'showoff.json', '.showoff.json.tmp' FileUtils.cp @varfile, 'showoff.json' end FileUtils.rm_rf('static') system("showoff static #{subject}") if File.exists? 'cobrand.png' FileUtils.mkdir(File.join('static', 'image')) FileUtils.cp('cobrand.png', File.join('static', 'image', 'cobrand.png')) end ensure FileUtils.mv('.showoff.json.tmp', 'showoff.json') if File.exist? '.showoff.json.tmp' end end |
#generate_pdf(subject) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/courseware/printer.rb', line 148 def generate_pdf(subject) # TODO screen printing # @pdfopts << " #{SCREENHACK}" # @suffix = "-screen" # Build the filename for the output PDF. This is ugly. output = File.join(@config[:output], "#{@prefix}-") output << "#{@variant}-" if @variant output << 'w-' if @config[:pdf][:watermark] case subject when :print output << "#{@version}.pdf" when :exercises, :solutions, :guide output << "#{@version}-#{subject}.pdf" else raise "I don't know how to generate a PDF of #{subject}." end generate_html(subject) FileUtils.mkdir(@config[:output]) unless File.directory?(@config[:output]) case @config[:renderer] when :wkhtmltopdf infofile = File.join(@config[:output], 'info.txt') scratchfile = File.join(@config[:output], 'scratch.pdf') command = ['wkhtmltopdf', '-s', 'Letter', '--print-media-type', '--quiet'] command << ['--footer-left', "#{@course} #{@version}", '--footer-center', '[page]'] command << ['--footer-right', "©#{Time.now.year} Puppet", '--header-center', '[section]'] command << ['--title', @course, File.join('static', 'index.html'), output] system(*command.flatten) raise 'Error generating PDF files' unless $?.success? if `pdftk #{output} dump_data | grep NumberOfPages`.chomp == 'NumberOfPages: 1' puts "#{output} is empty; aborting and cleaning up." FileUtils.rm(output) return end # We can't add metadata in the same run. It requires dumping, modifying, and updating if @event_id command = ['pdftk', output, 'dump_data', 'output', infofile] system(*command.flatten) raise 'Error retrieving PDF info' unless $?.success? info = File.read(infofile) File.open(infofile, 'w+') do |file| file.write("InfoBegin\n") file.write("InfoKey: Subject\n") file.write("InfoValue: #{@event_id}\n") file.write(info) end command = ['pdftk', output, 'update_info', infofile, 'output', scratchfile] system(*command.flatten) raise 'Error updating PDF info' unless $?.success? else FileUtils.mv(output, scratchfile) end command = ['pdftk', scratchfile, 'output', output] command << ['owner_pw', @config[:pdf][:password], 'allow', 'printing', 'CopyContents'] command << ['background', @watermark_pdf] if @config[:pdf][:watermark] command << ['user_pw', @password] if @password system(*command.flatten) raise 'Error watermarking PDF files' unless $?.success? FileUtils.rm(infofile) if File.exists? infofile FileUtils.rm(scratchfile) if File.exists? scratchfile when :prince command = ['prince', File.join('static', 'index.html')] command << ['--pdf-title', @course, '--pdf-author', @config[:pdf][:author], '--disallow-modify'] command << ['--pdf-subject', @event_id] if @event_id command << ['--style', @watermark_style] if @config[:pdf][:watermark] command << ['--encrypt', '--user-password', @password] if @password command << ['--license-file', @config[:pdf][:license]] if (@config[:pdf][:license] and File.exists? @config[:pdf][:license]) command << ['-o', output] system(*command.flatten) raise 'Error generating PDF files' unless $?.success? end FileUtils.rm_rf('static') end |
#guide ⇒ Object
64 65 66 67 68 |
# File 'lib/courseware/printer.rb', line 64 def guide $logger.info "Generating instructor guide pdf for #{@course} #{@version}..." generate_pdf(:guide) end |
#handouts ⇒ Object
46 47 48 49 50 |
# File 'lib/courseware/printer.rb', line 46 def handouts $logger.info "Generating handouts pdf for #{@course} #{@version}..." generate_pdf(:print) end |
#print ⇒ Object
40 41 42 43 44 |
# File 'lib/courseware/printer.rb', line 40 def print handouts exercises solutions end |
#solutions ⇒ Object
58 59 60 61 62 |
# File 'lib/courseware/printer.rb', line 58 def solutions $logger.info "Generating solutions guide pdf for #{@course} #{@version}..." generate_pdf(:solutions) end |