介紹
太新的wkhtmltopd版本不支持使用toc來生成目錄,wkhtmltopdf 0.12.3可以,它根據網頁中的H1、H2...標簽來確定目錄層級,所以網頁中要准備好。
wkhtmltopdf 12.03版本下載 http://wkhtmltopdf.org/
使用
wkhtmltopdf toc xxx.html xxxx.pdf 在xxxx.pdf 中生成目錄頁,如下所示:
格式不是很好看,二級目錄要比一級目錄小,如何修改目錄格式呢?
1.獲取目錄默認樣式
使用 wkhtmltopdf --dump-default-toc-xsl >myself.xsl 將目錄默認的樣式輸入到myself.xsl中,查看myself.xsl可以發現,目錄默認樣式中Table of Contents (h1)和一級目錄(ul) 用的都是20px arial的字體樣式,二級目錄的字體大小是一級的80%
2.修改默認的xsl
默認的 | 修改后的 |
<style> h1 { text-align: center; font-size: 20px; font-family: arial; } div {border-bottom: 1px dashed rgb(200,200,200);} span {float: right;} li {list-style: none;} ul { font-size: 20px; font-family: arial; } ul ul {font-size: 80%; } ul {padding-left: 0em;} ul ul {padding-left: 1em;} a {text-decoration:none; color: black;} </style> |
<style> h1 { text-align: left; font-size: 20px; font-family: calibri; } div {border-bottom: 1px dashed rgb(200,200,200);} span {float: right;} li {list-style: none;} ul { font-size: 12px; font-family: calibri; font-weight:bold } ul ul {font-size: 90%; } ul {padding-left: 0em;padding-top:5px;} ul ul {padding-left: 1em;} a {text-decoration:none; color: black;} </style> |
3.使用修改后的xsl生成目錄
wkhtmltopdf toc --xsl-style-sheet 'myself.xsl' xxx.html xxxx.pdf
默認的 | 修改后的 |
![]() |
![]() |
參考:https://www.jianshu.com/p/1a0fb28e6bec
https://www.cnblogs.com/mianbaoshu/p/13366074.html