Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Enable toc and cover options #158

Closed
wants to merge 3 commits into from

5 participants

@tim-vandecasteele

Don't prefix them with --, and allow metatags without content.

Moved --quiet to first command, otherwise it conflicted with the toc option when setting it.

Sadly the order in which you set the meta tags is important when using toc, because wkhtmltopdf needs the toc options to be grouped, and after the toc tag.

tim-vandecasteele added some commits
@tim-vandecasteele tim-vandecasteele toc and cover options are special cases.
Don't prefix them with --, and allow metatags without content.

Moved --quiet to first command, otherwise it conflicted with the toc option when setting it. Sadly the order in which you set the meta tags is important when using toc, because wkhtmltopdf needs the toc options to be grouped, and after the toc tag.
90f1433
@tim-vandecasteele tim-vandecasteele Take 1.8.7 hash behaviour into account for the spec. aecaed6
@tim-vandecasteele tim-vandecasteele Put the options in proper order.
'global options' 'cover' 'toc options' 'page options'.

This does have a disadvantage: you cannot specify specific page options for toc or cover, and you can't put a cover in the middle of a pdf.

For example, if you'd like to add a page-number to your TOC with a footer, that's currently not possible, because footer is a page option. This could be fixed by parsing the content of the 'toc' parameter, and translate that into other options.
0ff7839
@devn
Owner

Thanks for the commit. I will take a closer look within the next few days -- currently trying to catch up on open PRs and issues.

@jwang

Is there an update on this? Seems like a basic thing to add that can be supported. Also, should likely rebase since the original PR has been well over a year ago.

@sigmavirus24

I am the new maintainer of pdfkit and would happily consider this for inclusion. Thanks for the ping @jwang

@sigmavirus24

@tim-vandecasteele would you be willing to rebase these changes so they can be merged cleanly?

@rojotek

let me try another ping :) -- I'd love to see this merged in.

@tim-vandecasteele

I stopped using pdfkit, so I don't have this change rebased, sorry.

@sigmavirus24

@rojotek would you like to take this over? Ideally all you would need to do is rebase @tim-vandecasteele's commits onto master, but there may be other changes necessary after that. If you're not interested, I can put this on my list for this weekend or next.

@rojotek

ok -- grabbing this now -- will create a new PR with the rebased version shortly.

@sigmavirus24

Closing in preference of #255

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 1, 2012
  1. @tim-vandecasteele

    toc and cover options are special cases.

    tim-vandecasteele authored
    Don't prefix them with --, and allow metatags without content.
    
    Moved --quiet to first command, otherwise it conflicted with the toc option when setting it. Sadly the order in which you set the meta tags is important when using toc, because wkhtmltopdf needs the toc options to be grouped, and after the toc tag.
Commits on Dec 2, 2012
  1. @tim-vandecasteele
  2. @tim-vandecasteele

    Put the options in proper order.

    tim-vandecasteele authored
    'global options' 'cover' 'toc options' 'page options'.
    
    This does have a disadvantage: you cannot specify specific page options for toc or cover, and you can't put a cover in the middle of a pdf.
    
    For example, if you'd like to add a page-number to your TOC with a footer, that's currently not possible, because footer is a page option. This could be fixed by parsing the content of the 'toc' parameter, and translate that into other options.
This page is out of date. Refresh to see the latest.
Showing with 117 additions and 3 deletions.
  1. +37 −3 lib/pdfkit/pdfkit.rb
  2. +80 −0 spec/pdfkit_spec.rb
View
40 lib/pdfkit/pdfkit.rb
@@ -1,4 +1,11 @@
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
def initialize
@@ -31,15 +38,39 @@ def initialize(url_file_or_html, options = {})
def command(path = nil)
args = [executable]
- args += @options.to_a.flatten.compact
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?
args << '-' # Get HTML from stdin
else
args << @source.to_s
end
+ args += temp_options.to_a.flatten.compact
+
args << (path || '-') # Write to file or stdout
args.map {|arg| %Q{"#{arg.gsub('"', '\"')}"}}
@@ -87,7 +118,8 @@ def find_options_in_meta(content)
content.scan(/<meta [^>]*>/) do |meta|
if meta.match(/name=["']#{PDFKit.configuration.meta_tag_prefix}/)
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
@@ -115,7 +147,9 @@ def normalize_options(options)
options.each do |key, 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)
end
normalized_options
View
80 spec/pdfkit_spec.rb
@@ -135,6 +135,86 @@
pdfkit.command[pdfkit.command.index('"--orientation"') + 1].should == '"Landscape"'
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
context "#to_pdf" do
Something went wrong with that request. Please try again.