【问题标题】:wkhtmltopdf: Custom TOC style with --toc-header-text attributewkhtmltopdf:具有 --toc-header-text 属性的自定义 TOC 样式
【发布时间】:2017-06-23 17:50:12
【问题描述】:

我正在使用wkhtmltopdf 库生成带有选项的 PDF 文件

--toc-header-text TEXT --xsl-style-sheet config/wkhtmltopdf_toc.xsl

--toc-header-text 值未插入到生成的目录中。

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

【问题讨论】:

    标签: pdf xslt pdf-generation wkhtmltopdf tableofcontents


    【解决方案1】:

    使用 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 上搞砸了,直到它起作用为止。

    【讨论】:

      【解决方案2】:

      好的,这不是答案,但它正在解决问题

      我为每个生成的 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
      

      【讨论】:

        猜你喜欢
        • 2012-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多