wkhtmltopdf:带有 --toc-header-text 属性的自定义 TOC 样式

wkhtmltopdf: Custom TOC style with --toc-header-text attribute

我正在使用 wkhtmltopdf 库生成带有选项

的 PDF 文件
--toc-header-text TEXT --xsl-style-sheet config/wkhtmltopdf_toc.xsl

--toc-header-text 值未插入到生成的 table 内容中。

有没有办法将 --toc-header-text 变量的值插入到自定义目录样式表中?

好的,这不是答案,但它是解决方法

我为每个生成的 pdf 创建一个带有 toc 样式表的临时文件,用我的动态内容替换标题文本并将此文件传递给 wkhtmltopdf

在Ruby中可以是这样的:

toc_file = Tempfile.new
toc_content = File.
  read('config/wkhtmltopdf_toc.xsl').
  gsub(':index_title', title)
toc_file.write(toc_content)
toc_file.rewind

将文件路径传递给 wkhtmltopdf -- toc_file.path

生成pdf后销毁文件:

toc_file.close
toc_file.unlink

使用 XSLT 魔法根据第一个大纲项(即 TOC 本身)的名称定义变量。

我这样做了:

<xsl:variable name="tocTitle" select="//outline:outline/outline:item[1]/@title" />
<xsl:template match="outline:outline">
    <html>
    <head>
        <title><xsl:value-of select="$tocTitle" /></title>
    ...
    </head>
    <body>
        <h1><xsl:value-of select="$tocTitle" /></h1>
        <ul><xsl:apply-templates select="outline:item/outline:item"/></ul>
    </body>
    </html>
...

这是痛苦的尝试和错误(我不是这方面的专家,如果我做错了,很高兴有人纠正我)。我在 https://www.freeformatter.com/xsl-transformer.html 上搞砸了,直到它起作用。