7

I am generating a pdf using WKHTMLTOPDF with the table of content option, but it is adding itself to the table of contents, showing that it is on page 2.

Thoughts on removing this?

Adam D
  • 414
  • 4
  • 9

1 Answers1

13

You have full control over the TOC generation using XSL stylesheets for their generation. You can get the default stylesheet used by giving the argument --dump-default-toc-xsl to wkhtmltopdf.

When you examine it, you are particularly interested in the <body><h1>...</h1> H1 element and the test xsl:if test="(@title!='')"

For example, when I want to remove the TOC self-reference from itself, this is the relevant part of my stylesheet:

            stuff above
            <h1>My little TOC</h1>
            <ul><xsl:apply-templates select="outline:item/outline:item"/></ul>
        </body>
    </html>
</xsl:template>
<xsl:template match="outline:item">
    <li>
        <xsl:if test="(@title!='') and (@title!='My little TOC')">
        stuff below

When you save the new TOC XSL, you then need to reference it in your wkhtmltopdf arguments using something like --page-size A4 toc --xsl-style-sheet test.xsl TempFile.html TempFile.pdf.

Joel Peltonen
  • 13,025
  • 6
  • 64
  • 100
  • Perfect, just what I was looking for. Thanks! – kerrin Jun 16 '17 at 06:18
  • I tried to remove the title and author from the toc with this method, using their class but it seems that outline:item do not posses one. I've hard coded both but it a loosy workaround. Any idea to remove title and author from the TOC with a generic solution? – Fericelli Sep 07 '17 at 08:11
  • @Fericelli Sorry, I don't understand "using their class" means. Could you show a fiddle or some paste with your attempted TOC XSL file and the command you used? And also a brief explanation as to what should not be there. This method that I used should be very, very generic. – Joel Peltonen Sep 07 '17 at 15:10
  • Sorry for the lack of explanations, I have the doc title and author as h1 and h2 with custom class and they logically appears in the toc. I try to remove them by giving a class and test it in the toc xsl. here is the xsl : here is a part of the html file :

    Me

    My Title

    – Fericelli Sep 12 '17 at 05:01
  • can you please suggest how to do this in java? – Vikram Saini Feb 27 '18 at 06:28
  • @VikramSaini calling a process from java isn't really related to the original question at all, even if the issue there is with the TOC. Could you make a separate question out of it? – Joel Peltonen Feb 27 '18 at 13:23
  • is there way to use @ class @ attribute ? – famas23 Mar 11 '22 at 12:57
  • @famas23 no clue, sorry - I don't have a wkhtmltopdf setup ready to test :) Maybe you could try it? – Joel Peltonen Mar 11 '22 at 19:11