user503853
user503853

Reputation:

How to set the positon of table of content when using wkhtmltopdf

I'm using wkhtmltopdf to generate pdf from html pages.

My question is how to set the position of table of content page? It seems that it automatically generated in the beginning of first page. In addition, how to set the css of content of content?

Upvotes: 4

Views: 10071

Answers (2)

ppant
ppant

Reputation: 762

If you want you can even make your customized TOC using a html file. e.g.; if you want to create TOC on names of html file name(s) which will be used in PDF creation (please note that for this you should know names in advance) then you can do this by passing a HTML file say user_toc.html. In this files you can put all your captions/css etc and make a placeholder for file name. This files needs to be parsed with server side code which should fill the file name in placeholder. Now the modified file can be used for TOC.

Example code in Perl:

    my $TOCPage = "user_toc.html";
    my $file1 = "Testfile1.html";
    my $file2 = "Testfile2.html";
    my $toc_file_lines;

    # Open the user TOC for parsing and write in a buffer
    open(FILE, $TOCPage);
    read(FILE, $toc_file_lines, -s $TOCPage);
    close(FILE);

    # Substitute the placeholder with actual file name
    $toc_file_lines =~ s/$file_name_placeholder/$file1/;
    # Open the same file again and write back the buffer
    open(FILE, ">".$TOCPage);
    print FILE $toc_file_lines;
    close(FILE);

    # Linux path for wkhtmltopdf installation
    my $wkhtmltopdf_path = '/usr/bin/wkhtmltopdf-i386';  
    my $command = "$wkhtmltopdf_path --margin-top 20mm --margin-bottom 15mm 
                                    --margin-left 15mm --margin-right 15mm 
                                    $TOCPage $file1 $file2 $pdf_manual_name"; 
   `$command 2>&1`;

More over you can add a lot other info in TOC like chapter no/total page in chapter so on.

Hope this helps.

Upvotes: 3

Mark Storer
Mark Storer

Reputation: 15868

There's an--xsl-style-sheet (file) parameter to wkhtmltopdf, detailed thusly in the extended command line --help (or -H).

A table of content can be added to the document by adding a toc object to
the command line. For example:  

  wkhtmltopdf toc http://doc.trolltech.com/4.6/qstring.html qstring.pdf

The table of content is generated based on the H tags in the input
documents. First a XML document is generated, then it is converted to
HTML using XSLT.

The generated XML document can be viewed by dumping it to a file using
the --dump-outline switch. For example:

  wkhtmltopdf --dump-outline toc.xml http://doc.trolltech.com/4.6/qstring.html qstring.pdf

The XSLT document can be specified using the --xsl-style-sheet switch.
For example:

  wkhtmltopdf toc --xsl-style-sheet my.xsl http://doc.trolltech.com/4.6/qstring.html qstring.pdf

The --dump-default-toc-xsl switch can be used to dump the default
XSLT style sheet to stdout. This is a good start for writing your
own style sheet

  wkhtmltopdf --dump-default-toc-xsl

The XML document is in the namespace 
  http://code.google.com/p/wkhtmltopdf/outline

it has a root node called "outline" which contains a number of
"item" nodes. An item can contain any number of item. These are the
outline subsections to the section the item represents. A item node
has the following attributes:

 - "title" the name of the section
 - "page" the page number the section occurs on
 - "link" a URL that links to the section.
 - "backLink" the name of the anchor the the section will link back to.

The remaining TOC options only affect the default style sheet
so they will not work when specifying a custom style sheet.

So you define your own XSLT, possibly based on their default, and pass it in. No problemo.

Upvotes: 7

Related Questions