Skip to content

Instantly share code, notes, and snippets.

@kristianmandrup
Last active May 31, 2019 09:29
Show Gist options
  • Save kristianmandrup/5280569 to your computer and use it in GitHub Desktop.
Save kristianmandrup/5280569 to your computer and use it in GitHub Desktop.
Jekyll PDF Converter using PDFKit
require 'pdfkit'
require 'nokogiri'
require 'haml'
# config/initializers/pdfkit.rb
PDFKit.configure do |config|
# Note: Often required for Windows OS configuration
# config.wkhtmltopdf = '/path/to/wkhtmltopdf'
# Basic configuration/customization
# config.default_options = {
# :page_size => 'Legal',
# :print_media_type => true
# }
# config.root_url = "http://localhost" # Use only if your external hostname is unavailable on the server.
end
module Jekyll
class PdfConverter < Converter
safe true
priority :low
def matches(ext)
ext =~ /\.pdf$/i
end
def output_ext(ext)
".pdf"
end
def convert(content)
styled_kit.to_pdf
end
protected
# override to assemble the specific part of the page to print to the PDF document!
#
def page
content
end
def styled_kit
css_files.compact.each do |css_ref|
kit.stylesheets << css_ref
end
end
def css_files
[]
end
def kit
@kit ||= PDFKit.new(page, :page_size => 'Letter')
end
def page_filter
@page_filter ||= PageFilter.new content
end
class PageFilter
attr_reader :content
def initialize content
@content = content
end
def doc
@doc ||= Nokogiri::HTML(content)
end
def layout
haml %Q{
!!!
html
body
= page_content
}
end
def page
layout.render page_content: page_content
end
# filter out the part of the page to use for PDF document
# See Nokogiri docs
# Tutorials:
# - http://ruby.bastardsbook.com/chapters/html-parsing/
# - https://blog.engineyard.com/2010/getting-started-with-nokogiri
def page_content
doc.css('section#page-content')
end
end
end
end
@gamort
Copy link

gamort commented Jul 5, 2016

To run with begin.rb

Place begin.rb in the directory _plugins
Run bin/jekyll

Done.

I also had to add 2 lines to the file: Gemfile in the main directory:
gem "pdfkit"
gem "haml"

Lastly I removed Gemfile.lock just because I thought it might be needed and ran "bundler install" again
It installed some more stuff and then when I ran "bin/jekyll" it loaded fine.

My guess is that there should be a button/menu item somewhere that will generate the PDF. Right now it is generated the docs so I don't know yet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment