Last active
May 31, 2019 09:29
-
-
Save kristianmandrup/5280569 to your computer and use it in GitHub Desktop.
Jekyll PDF Converter using PDFKit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
I have added the file in _plugins
directory on jeykll.
Updated the configuration as per my setup. (Installed wkhtmltopdf
as required).
PDFKit.configure do |config|
# Note: Often required for Windows OS configuration
config.wkhtmltopdf = 'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'
# 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
Here is how my file looks like.
require 'pdfkit'
require 'nokogiri'
require 'haml'
# config/initializers/pdfkit.rb
PDFKit.configure do |config|
# Note: Often required for Windows OS configuration
config.wkhtmltopdf = 'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'
# 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
I am not sure where the pdf files are generated.
Any help is appreciated. Thanks.
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
does someone has a tutorial/document on how to use this in jekyll.
Thanks.