答案 0 :(得分:0)
我的项目中不得不面对类似的问题。
您有2种方法可以解决问题:
一种方法是使用CSS解决它,但wkhtmltopdf中包含的webkit版本(至少是稳定版本)似乎有点旧,并且不能很好地支持CSS多列指令,{{ 3}}。
为了将来参考,我复制并粘贴链接问题中包含的示例模板:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<style type="text/css">
body {
margin: 0;
padding: 0;
border: none;
width: 17cm;
}
.container {
/* You *must* define a fixed height which is
large enough to fit the whole content,
otherwise the layout is unpredictable. */
height: 28em;
/* Width and count aren't respected, but you
have to give at least some dummy value (??). */
-webkit-columns: 0 0;
/* This is the strange way to define the number of columns:
50% = 2 columns, 33% = 3 columns 25% = 4 columns */
width: 33%;
/* Gap and rule do work. */
-webkit-column-gap: 2em;
-webkit-column-rule: 1px solid black;
text-align: justify;
}
</style>
</head>
<body>
<div class="container">
<h1>An Article</h1>
<p>
1. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<p>
2. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<p>
3. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<p>
4. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</div>
</body>
</html>
&#13;
另一个选项(我最终使用的那个)是使用带有2..n列的表来设置页面的布局。然后,您可以使用模板引擎甚至JavaScript来填充包含内容的页面并拆分页面。
如果页面中的内容具有可预测的大小,这种方法很简单并且效果很好,实际上你不能依靠wkhtmltopdf将内容分成几个页面,你必须自己使用代码。
方法1)非常错误,但如果您处理文本可能值得一试,这可能不太适合方法2)。
如果你的内容是常规的,那么跳上方法2)并放下元素并使用代码拆分页面将相当容易。