Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable toc and cover options #158

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 37 additions & 3 deletions lib/pdfkit/pdfkit.rb
@@ -1,4 +1,11 @@
class PDFKit class PDFKit
GLOBAL_OPTIONS = ["--collate", "--grayscale", "--page-size", "--page-height",
"--orientation", "--lowquality", "--margin-top", "--margin-right",
"--margin-bottom", "--margin-left","--title", '--encoding',
"--disable-smart-shrinking"]

TOC_OPTIONS = ["--disable-dotted-lines", "--toc-header-text", "--toc-level-indentation",
"--disable-toc-links", "--toc-text-size-shrink", "--xsl-style-sheet"]


class NoExecutableError < StandardError class NoExecutableError < StandardError
def initialize def initialize
Expand Down Expand Up @@ -31,15 +38,39 @@ def initialize(url_file_or_html, options = {})


def command(path = nil) def command(path = nil)
args = [executable] args = [executable]
args += @options.to_a.flatten.compact
args << '--quiet' args << '--quiet'


temp_options = @options.clone


global_options = temp_options.to_a - temp_options.delete_if{|key, value| GLOBAL_OPTIONS.include?(key) }.to_a
if global_options
args += global_options.flatten.compact
end

if temp_options.has_key?("cover")
temp_option = temp_options.delete("cover")
args += {"cover" => temp_option}.to_a.flatten.compact
end

if temp_options.has_key?("toc")
temp_option = temp_options.delete("toc")
args += {"toc" => temp_option}.to_a.flatten.compact
end

toc_options = temp_options.to_a - temp_options.delete_if{|key, value| TOC_OPTIONS.include?(key) }.to_a
if toc_options
args += toc_options.flatten.compact
end

if @source.html? if @source.html?
args << '-' # Get HTML from stdin args << '-' # Get HTML from stdin
else else
args << @source.to_s args << @source.to_s
end end


args += temp_options.to_a.flatten.compact

args << (path || '-') # Write to file or stdout args << (path || '-') # Write to file or stdout


args.map {|arg| %Q{"#{arg.gsub('"', '\"')}"}} args.map {|arg| %Q{"#{arg.gsub('"', '\"')}"}}
Expand Down Expand Up @@ -87,7 +118,8 @@ def find_options_in_meta(content)
content.scan(/<meta [^>]*>/) do |meta| content.scan(/<meta [^>]*>/) do |meta|
if meta.match(/name=["']#{PDFKit.configuration.meta_tag_prefix}/) if meta.match(/name=["']#{PDFKit.configuration.meta_tag_prefix}/)
name = meta.scan(/name=["']#{PDFKit.configuration.meta_tag_prefix}([^"']*)/)[0][0] name = meta.scan(/name=["']#{PDFKit.configuration.meta_tag_prefix}([^"']*)/)[0][0]
found[name.to_sym] = meta.scan(/content=["']([^"']*)/)[0][0] meta_content = meta.scan(/content=["']([^"']*)/)[0]
found[name.to_sym] = meta_content ? meta_content[0] : true
end end
end end


Expand Down Expand Up @@ -115,7 +147,9 @@ def normalize_options(options)


options.each do |key, value| options.each do |key, value|
next if !value next if !value
normalized_key = "--#{normalize_arg key}" normalized_key = normalize_arg(key)
normalized_key = "--#{normalized_key}" unless ['toc','cover'].include?(normalized_key)

normalized_options[normalized_key] = normalize_value(value) normalized_options[normalized_key] = normalize_value(value)
end end
normalized_options normalized_options
Expand Down
80 changes: 80 additions & 0 deletions spec/pdfkit_spec.rb
Expand Up @@ -135,6 +135,86 @@
pdfkit.command[pdfkit.command.index('"--orientation"') + 1].should == '"Landscape"' pdfkit.command[pdfkit.command.index('"--orientation"') + 1].should == '"Landscape"'
end end


it "should not prefix cover and toc meta tags" do
body = %{
<html>
<head>
<meta name="pdfkit-toc" content="Toc" />
<meta name="pdfkit-cover" content="some.html"/>
</head>
</html>
}
pdfkit = PDFKit.new(body)
pdfkit.command[pdfkit.command.index('"toc"') + 1].should == '"Toc"'
pdfkit.command[pdfkit.command.index('"cover"') + 1].should == '"some.html"'
end

it "should work for meta tags without content" do
body = %{
<html>
<head>
<meta name="pdfkit-default-header" />
<meta name="pdfkit-javascript-delay" content="20" />
</head>
</html>
}
pdfkit = PDFKit.new(body)
pdfkit.command[pdfkit.command.index('"--default-header"') + 1][0..2].should == '"--'
end

it "should put toc option just before the page and page options" do
body = %{
<html>
<head>
<meta name="pdfkit-toc" />
<meta name="pdfkit-javascript-delay" content="20" />
</head>
</html>
}
pdfkit = PDFKit.new(body)
pdfkit.command[pdfkit.command.index('"toc"') + 1].should == '"-"'
end

it "should put a toc-option right after toc" do
body = %{
<html>
<head>
<meta name="pdfkit-toc" />
<meta name="pdfkit-javascript-delay" content="20" />
<meta name="pdfkit-xsl-style-sheet" content="toc.xsl"/>
</head>
</html>
}
pdfkit = PDFKit.new(body)
pdfkit.command[pdfkit.command.index('"toc"') + 1].should == '"--xsl-style-sheet"'
end

it "should put cover before page and page options" do
body = %{
<html>
<head>
<meta name="pdfkit-cover" content="cover.html" />
<meta name="pdfkit-javascript-delay" content="20" />
</head>
</html>
}
pdfkit = PDFKit.new(body)
pdfkit.command[pdfkit.command.index('"cover"') + 2].should == '"-"'
end

it "should work for meta tags without content" do
body = %{
<html>
<head>
<meta name="pdfkit-toc" />
<meta name="pdfkit-orientation" content="Landscape" />
</head>
</html>
}
pdfkit = PDFKit.new(body)
pdfkit.command[pdfkit.command.index('"toc"') + 1][0..2].should == '"-"'
end

end end


context "#to_pdf" do context "#to_pdf" do
Expand Down