4

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

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

4

2 回答 2

7

--xsl-style-sheet (file)wkhtmltopdf有一个参数,因此在扩展命令行--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,可能基于它们的默认值,然后将其传入。没问题。

于 2011-05-10T18:08:38.507 回答
3

如果您愿意,您甚至可以使用 html 文件制作自定义的 TOC。例如; 如果您想在创建 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 中添加许多其他信息,例如章节中的章节编号/总页数等等。

希望这可以帮助。

于 2012-03-01T11:53:27.987 回答