-
-
Save kristianmandrup/5280569 to your computer and use it in GitHub Desktop.
| 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 |
Id be keen to use this as well.
How to use?
I'm going to try and get this into a proper plugin as I prefer wktopdf options to latex / pandoc plugins.
Bear with me :)
Follow along here - https://github.com/ChrisChinchilla/wkhtmltopdf-for-Jekyll
does someone has a tutorial/document on how to use this in jekyll.
Thanks.
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.
endHere 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
endI 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
Seconded. I was going to post some ideas, but got stumped by requirements. I think something is missing as this is just a gist.
If you can add some pointers on how to get this started I will be happy to adapt this to a full plugin as i'd love to get wktohtml working with jekyll.