【问题标题】:How to set the positon of table of content when using wkhtmltopdf使用wkhtmltopdf时如何设置目录的位置
【发布时间】:2023-03-30 17:40:01
【问题描述】:

我正在使用 wkhtmltopdf 从 html 页面生成 pdf。

我的问题是如何设置目录页面的位置?它似乎在第一页的开头自动生成。另外,如何设置content的css

【问题讨论】:

    标签: pdf-generation wkhtmltopdf


    【解决方案1】:

    wkhtmltopdf 有一个--xsl-style-sheet (file) 参数,详细说明在扩展命令行 --help(或 -H)中。

    可以通过添加 toc 对象来将目录添加到文档中 命令行。例如: wkhtmltopdf toc http://doc.trolltech.com/4.6/qstring.html qstring.pdf 目录是根据输入中的H标签生成的 文件。首先生成一个 XML 文档,然后将其转换为 使用 XSLT 的 HTML。 生成的 XML 文档可以通过将其转储到文件中来查看 --dump-outline 开关。例如: wkhtmltopdf --dump-outline toc.xml http://doc.trolltech.com/4.6/qstring.html qstring.pdf 可以使用 --xsl-style-sheet 开关指定 XSLT 文档。 例如: wkhtmltopdf toc --xsl-style-sheet my.xsl http://doc.trolltech.com/4.6/qstring.html qstring.pdf --dump-default-toc-xsl 开关可用于转储默认值 XSLT 样式表到标准输出。这是编写你的一个好的开始 自己的样式表 wkhtmltopdf --dump-default-toc-xsl XML 文档位于命名空间中 http://code.google.com/p/wkhtmltopdf/outline 它有一个名为“outline”的根节点,其中包含许多 “项目”节点。一个项目可以包含任意数量的项目。这些是 勾勒出项目所代表的部分的小节。一个项目节点 具有以下属性: -“标题”部分的名称 - "page" 该部分出现的页码 - “链接”链接到该部分的 URL。 - “backLink” 该部分将链接回的锚点的名称。 其余 TOC 选项仅影响默认样式表 因此在指定自定义样式表时它们将不起作用。

    所以您定义自己的 XSLT,可能基于它们的默认值,然后将其传入。没问题。

    【讨论】:

      【解决方案2】:

      如果您愿意,您甚至可以使用 html 文件制作自定义目录。例如。;如果您想在创建 PDF 时使用的 html 文件名的名称上创建 TOC(请注意,为此您应该事先知道名称),那么您可以通过传递一个 HTML 文件来执行此操作,例如 user_toc.html。在此文件中,您可以放置​​所有字幕/css 等,并为文件名创建一个占位符。该文件需要使用服务器端代码进行解析,该代码应在占位符中填充文件名。现在修改后的文件可用于 TOC。

      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`;
      

      此外,您还可以在 TOC 中添加许多其他信息,例如章节编号/章节总页数等。

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-19
        • 1970-01-01
        • 2013-05-25
        • 1970-01-01
        • 2011-01-09
        • 2016-04-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多