python-pdfkit(wkhtmltopdf)TOC溢出

时间:2018-05-15 17:56:15

标签: python xslt wkhtmltopdf xsl-fo python-pdfkit

我目前正在创建一个非常好的PDF。它在技术上没有任何问题。然而,TOC很难看。

TOC是通过xsl生成的,它通过jinja2传递给页面顶部的简单细节。我修改了XSL以精确匹配客户的品牌和设计。然而,该名单的高度不断增加。

这是当前的结果(抱歉模糊文字)你可以看到toc在新页面的正确位置拾取,但似乎无法应用上边距到新页面: enter image description here

代码: 这是xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:outline="http://wkhtmltopdf.org/outline"
            xmlns="http://www.w3.org/1999/xhtml">
  <xsl:output doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
          doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
          indent="yes" />
  <xsl:template match="outline:outline">
    <html>
      <head>
        <title>Table of Contents</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <style>

      body{
        background-color: #fff;
        margin-left: 0px;
        margin-top: 0px;
        color:#1e1e1e;
        font-family: arial, verdana,sans-serif;
        font-size: 90px;
      }
      .contentSection{
        position:relative;
        height:3200px;
        width:6100px;
      }
      .profile{
        position:absolute;
        display:inline-block;
        top:200px !important;
      }


      h1 {
        text-align: left;
        font-size: 70px;
        font-family: arial;
        color: #ef882d;
      }
      li {
        border-bottom: 1px dashed rgb(45,117,183);
      }
      span {float: right;}
      li {
        list-style: none;
        margin-top:30px;
      }
      ul {
        font-size: 70px;
        font-family: arial;
        color:#2d75b7;
      }

      ul ul {font-size: 80%; padding-top:0px;}
      ul {padding-left: 0em; padding-top:0px;}
      ul ul {padding-left: 1em; padding-top:0px;}
      a {text-decoration:none; color: color:#2d75b7;}


      #topper{
        width:100%;
        border-bottom:8px solid #ef882d;
      }
      #title{
        position:absolute;
        top:60px;
        font-size:60px;
        left:150px;
        color:#666666;
      }

      h1, h2{
        font-size:60px;
        -webkit-margin-before: 0px;
        -webkit-margin-after: 0px;
        -webkit-margin-start: 0px;
        -webkit-margin-end: 0px;
      }


      #profile{
        position:static;
        -webkit-border-top-left-radius: 40px;
        -webkit-border-bottom-left-radius: 40px;
        -moz-border-radius-topleft: 40px;
        -moz-border-radius-bottomleft: 40px;
        border-top-left-radius: 40px;
        border-bottom-left-radius: 40px;
        right:-540px;
        background-color: #2d75b7;
        padding:4px;
        padding-left:60px;
        padding-right:250px;
        color:#fff;
        display:inline-block;
        margin-top:200px;
        float:right;
      }

      #room{
        padding-top: 200px;
        padding-left: 150px;
        display:inline-block;
      }
      #section{
        padding-left: 150px;
        color: #ef882d;
        text-transform: uppercase;
        font-size:60px;
        font-weight: bold;
        display:inline-block;
        margin-top: 30px;
        margin-bottom: 5px;
      }
      #area{
        padding-left: 150px;
        font-size:60px;
        color:#2d75b7;
        margin-top: 15px;
      }
      #dims{
        padding-left: 150px;
        font-size:60px;
        color:#2d75b7;
        margin-top: 15px;
      }
      #toc{
        width:50%;
        margin-top:150px;
        margin-left:300px;
      }
    </style>
    <script>
      var value = {{profile|e}};
    </script>
  </head>
  <body>
    <div class="contentSection">
      <div id="title">A title here</div>
      <div id="topper">
        <div id="profile" class="profile">{{profile|e}}</div>
        <div id="room"> {{profile|e}} </div>
        <div id="area"> Revision Date </div>
        <div id="dims"> {{area|e}} </div>
        <div id="section">Table of Contents</div>
      </div>
      <div id="toc">
        <ul><xsl:apply-templates select="outline:item/outline:item"/></ul>
      </div>
    </div>
  </body>
</html>
 </xsl:template>
  <xsl:template match="outline:item">
    <! begin LI>
    <li>
      <xsl:if test="@title!=''">
        <div>
          <a>
            <xsl:if test="@link">
              <xsl:attribute name="href"><xsl:value-of select="@link"/> . 
 </xsl:attribute>
            </xsl:if>
            <xsl:if test="@backLink">
              <xsl:attribute name="name"><xsl:value-of select="@backLink"/> .   </xsl:attribute>
            </xsl:if>
            <xsl:value-of select="@title" />
          </a>
          <span>
            <xsl:value-of select="@page" />
          </span>
        </div>
      </xsl:if>
      <ul>
        <xsl:comment>added to prevent self-closing tags in QtXmlPatterns</xsl:comment>
        <xsl:apply-templates select="outline:item"/>
      </ul>
    </li>
  </xsl:template>
</xsl:stylesheet>

我使用传统的HTML,JavaScript和文档就绪标记处理了PDF其他区域的内容溢出。然而,TOC需要一个XSL文件。

我尝试用nth-child css nth-child被忽略。

问题:

* wkhtmltopdf或python pdf-kit中是否有办法专门处理TOC中的分页符,并在新页面上放置更好的页边距?有没有办法提供TOC作为传统的html页面,以便我可以用javaScript来做到这一点? *

1 个答案:

答案 0 :(得分:2)

代码审查

我在您的XSL(和CSS)文件中进行了快速代码审查。 即使它不能解决您的问题,它也有助于重现和理解它。 这是我的评论:

  • 您的XSL有一个错字:<! begin LI>不是有效的XML选项卡。是评论吗?

  • 我更喜欢使用concat() XPath函数直接附加字符。因为,如果您重新缩进代码,则可能会引入额外的空格。

    所以,我更换了:

    <xsl:attribute name="href"><xsl:value-of select="@link"/> . </xsl:attribute>
    

    通过:

    <xsl:attribute name="href">
      <xsl:value-of select="concat(@link, ' . ')"/>
    </xsl:attribute>
    
  • 我添加了xs:if,以防止在不需要时生成空的<ul>

    <xsl:if test="count(outline:item)">
      <ul>
        <xsl:comment>added to prevent self-closing tags in QtXmlPatterns</xsl:comment>
        <xsl:apply-templates select="outline:item"/>
      </ul>
    </xsl:if>
    
  • 我还修复了重复的或格式错误的CSS条目,我替换为:

    li {
      border-bottom: 1px dashed rgb(45, 117, 183);
    }
    
    span {
      float: right;
    }
    
    li {
      list-style: none;
      margin-top: 30px;
    }
    
    ul ul {font-size: 80%; padding-top:0px;}
    ul {padding-left: 0em; padding-top:0px;}
    ul ul {padding-left: 1em; padding-top:0px;}
    a {text-decoration:none; color: color:#2d75b7;}
    

    作者:

    span {
      float: right;
    }
    
    li {
      list-style: none;
      margin-top: 30px;
      border-bottom: 1px dashed rgb(45, 117, 183);
    }
    
    ul {
        font-size: 70px;
        font-family: arial;
        color: #2d75b7;
    }
    
    ul ul {
        font-size: 80%;
        padding-left: 1em;
        padding-top: 0px;
    }
    
    a {
        text-decoration: none;
        color: #2d75b7;
    }
    
    
  • 如果您以XHTML为目标,则<style>标记具有强制性的type属性。对<script>属性的说明相同。

    <style type="text/css">...</style>
    <script type="text/javascript">...</script>
    

重现问题

由于缺少信息,因此很难重现您的错误。所以我猜到了。

首先,我创建一个示例TOC文件,如下所示:

outline.xml

<?xml version="1.0" encoding="UTF-8"?>
<outline xmlns="http://wkhtmltopdf.org/outline">
  <item>
    <item title="Lorem ipsum dolor sit amet, consectetur adipiscing elit." page="2"/>
    <item title="Cras at odio ultrices, elementum leo at, facilisis nibh." page="8"/>
    <item title="Vestibulum sed libero bibendum, varius massa vitae, dictum arcu." page="19"/>
    ...
    <item title="Sed semper augue quis enim varius viverra." page="467"/>
  </item>
</outline>

此文件包含70个项目,因此我可以看到分页符。

要构建HTML和PDF,我使用了(固定的)XSL文件并运行pdfkit:

import io
import os

import pdfkit
from lxml import etree

HERE = os.path.dirname(__file__)


def layout(src_path, dst_path):
    # load the XSL
    xsl_path = os.path.join(HERE, "layout.xsl")
    xsl_tree = etree.parse(xsl_path)

    # load the XML source
    src_tree = etree.parse(src_path)

    # transform
    transformer = etree.XSLT(xsl_tree)
    dst_tree = transformer.apply(src_tree)

    # write the result
    with io.open(dst_path, mode="wb") as f:
        f.write(etree.tostring(dst_tree, encoding="utf-8", method="html"))


if __name__ == '__main__':
    layout(os.path.join(HERE, "outline.xml"), os.path.join(HERE, "outline.html"))
    pdfkit.from_file(os.path.join(HERE, "outline.html"),
                     os.path.join(HERE, "outline.pdf"),
                     options={'page-size': 'A1', 'orientation': 'landscape'})

注意:您的页面大小看起来非常大……

解决方案

您是对的,wkhtmltopdf没有考虑CSS的边距:

li {
  list-style: none;
  border-bottom: 1px dashed rgb(45, 117, 183);
  margin-top: 30px;  # <-- not working after page break
}

这是正常现象,例如考虑标题段(h1h2等)。 标头可以使用顶部空白,以便在段落和后续标头之间添加空格, 但是,如果页眉开始一个新页面,我们希望摆脱页边空白,而标题移到页面顶部空白。

对于您的目录,有一个解决方案。您可以使用padding(而不是margin):

li {
  border-bottom: 1px dashed rgb(45, 117, 183);
  list-style: none;
  padding-top: 30px;
}

实际上,TOC内容(#toc元素)是固定的:

#toc {
  width: 50%;
  margin-top: 150px;
  margin-left: 300px;
}

因此,您可以减少margin-top来满足需要,例如:

#toc {
  width: 50%;
  margin-top: 120px;
  margin-left: 300px;
}