Pdf generation 使用wkhtmltopdf时如何设置目录的位置

Pdf generation 使用wkhtmltopdf时如何设置目录的位置,pdf-generation,wkhtmltopdf,Pdf Generation,Wkhtmltopdf,我正在使用wkhtmltopdf从html页面生成pdf 我的问题是如何设置目录页面的位置?它似乎是在第一页的开头自动生成的。此外,如何设置内容内容的css?wkhtmltopdf有一个--xsl样式表(文件)参数,详细信息请参见扩展命令行--help(或-H) 通过向文档中添加toc对象,可以将目录添加到文档中 命令行。例如: WKHTMLTOC PDF目录http://doc.trolltech.com/4.6/qstring.html qstring.pdf 内容表是根据输入中的H标记生成

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

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

wkhtmltopdf有一个
--xsl样式表(文件)
参数,详细信息请参见扩展命令行--help(或-H)

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

如果您愿意,您甚至可以使用html文件定制TOC。例如。;如果您想在PDF创建中使用的html文件名的名称上创建TOC(请注意,为此您应该提前知道名称),那么您可以通过传递一个html文件(比如
user\u 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中添加许多其他信息,如章节中的章节号/总页面等

希望这有帮助

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