class Middleman::PDFKit::Extension

Public Class Methods

new(klass, options_hash={}, &block) click to toggle source
Calls superclass method
# File lib/middleman-pdfkit/extension.rb, line 17
def initialize(klass, options_hash={}, &block)
  @prefix = "build/"

  super
  setup_filenames
end

Public Instance Methods

after_build(builder) click to toggle source
# File lib/middleman-pdfkit/extension.rb, line 34
def after_build(builder)
  @filenames.each do |file, output|
    puts "after_build: [#{file}] > [#{output}]"
    # build_pdf_for(file, "#{@prefix}#{output}")
    build_pdf_for(file, output)
  end
end
manipulate_resource_list(resources) click to toggle source
# File lib/middleman-pdfkit/extension.rb, line 24
def manipulate_resource_list(resources)
  # Add my @filenames to Sitemap
  @filenames.each do |input, output|
    resources << Middleman::Sitemap::Extensions::EndpointResource.new(app.sitemap, output, "/")
  end

  # Return the new list
  resources
end

Private Instance Methods

all_html_files() click to toggle source
# File lib/middleman-pdfkit/extension.rb, line 92
def all_html_files
  # TODO: find a better way?!
  Dir.glob(File.join(@prefix, '**', '*.html')).map do |d|
    File.join(File.dirname(d).sub(@prefix, ''), File.basename(d, '.html'))[1..-1]
  end
end
build_pdf_for(input, output) click to toggle source
# File lib/middleman-pdfkit/extension.rb, line 44
def build_pdf_for input, output
  # Output file should be prefixed only here
  input1 = "#{@prefix}#{input}"
  input2 = "#{@prefix}#{input}.html"
  outfile = "#{@prefix}#{output}"

  # Test input flies presence
  if File.exist?(input1)
    puts "create", outfile
    generate_pdf(input1, outfile)

  elsif File.exist?(input2)
    puts "create", outfile
    generate_pdf(input2, outfile)

  else
    puts "error", "PDFKit: none of source HTML files [#{file1}, #{file2}] been found", :red
  end

end
generate_pdf(html_filename, pdf_filename) click to toggle source
# File lib/middleman-pdfkit/extension.rb, line 99
def generate_pdf(html_filename, pdf_filename)
  kit = ::PDFKit.new(File.new(html_filename),
    disable_smart_shrinking:  options.disable_smart_shrinking,
    quiet:                    options.quiet,
    page_size:                options.page_size,
    margin_top:               options.margin_top,
    margin_right:             options.margin_right,
    margin_bottom:            options.margin_bottom,
    margin_left:              options.margin_left,
    print_media_type:         options.print_media_type,
    encoding:                 options.encoding)
  file = kit.to_file(pdf_filename)
  # puts "pdfkit: #{kit.command}"
end
setup_filenames() click to toggle source
# File lib/middleman-pdfkit/extension.rb, line 65
def setup_filenames
  # Init hash of file vectors
  @filenames = {}

  # If no filenames provided let's assume all detected files
  if !options.filenames || options.filenames.empty?
    all_html_files.filenames.each do |file|
      @filenames[file] = "#{file}.pdf"
    end

  # If it's a legacy array, let's build the destination filename
  elsif options.filenames.is_a?(Array)
    options.filenames.each do |file|
      @filenames[file] = "#{file}.pdf"
    end

  # If it's a new hash, the destination filename is provided
  elsif options.filenames.is_a?(Hash)
    options.filenames.each do |file, output|
      @filenames[file] = "#{output}"
    end

  end

  @filenames
end