WKHTMLTOPDF:如何在第一页禁用页眉 [英] WKHTMLTOPDF: How to disable header on the first page

查看:42
本文介绍了WKHTMLTOPDF:如何在第一页禁用页眉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

wkhtml 不会像应有的那样在每个页面上重复表格元素th".所以我认为可以简单地使用 --header-html 选项并以这种方式手动添加表头.但我不希望它们出现在第一页,因为已经有表头,加上其他一些第一页的东西......我找到了一些 JS 解决方案,但它对我来说太复杂了,因为我只知道非常基础的JS...有什么想法吗?

wkhtml doesn´t repeat table elements "th" on every page like it should. So I thought it could be possible to simply use the --header-html option and add the table headers manually this way. But I don´t want them on the first page, since there are table headers already, plus some other first page stuff... I found some JS solution, but its too much complicated for me, since I know just the very basics of JS... Any ideas?

推荐答案

你试过 JS 解决方案了吗?其实没那么复杂.我刚刚对一个长 html 文件进行了测试,该文件包含一个分成许多不同页面的表,我设法使用这个头文件从第 1 页和第 3 页中删除了标题:

Did you try the JS solution? It's actually not that complicated. I just did a test with a long html file that contained a table that is split into many different pages and I managed to remove the headers from page 1 and 3 using this header file:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <script>
        function subst() {
          var vars={};
          var x=document.location.search.substring(1).split('&');
          for (var i in x) {var z=x[i].split('=',2);vars[z[0]] = unescape(z[1]);}
          var x=['frompage','topage','page','webpage','section','subsection','subsubsection'];
          for (var i in x) {
            var y = document.getElementsByClassName(x[i]);
            for (var j=0; j<y.length; ++j) y[j].textContent = vars[x[i]];

            if(vars['page'] == 1){ // If page is 1, set FakeHeaders display to none
               document.getElementById("FakeHeaders").style.display = 'none';
            }

            if(vars['page'] == 3) { // If page is 3, set FakeHeaders display to none
                document.getElementById("FakeHeaders").style.display = 'none';
            }
          }
        }
        </script>
    </head>
    <body style="border:0;margin:0;" onload="subst()">
        <table style="border-bottom: 1px solid pink; width: 100%; margin-bottom:5px;" id="FakeHeaders">
          <tr>
            <th>Your awesome table column header 1</th>
            <th>Column 2</th>
            <th style="text-align:right">
                Page <span class="page"></span>/<span class="topage"></span>
            </th>
          </tr>
        </table>
    </body>
</html>

他们的关键点不是表标题"包含在 ID 为FakeHeaders"的表中.javascript 函数 subst() 在加载正文时运行,并在函数期间检查当前页面是否为 1 或当前页面是否为 3,如果是,则将 FakeHeaders 设置为不可见.您将需要使用边距和 CSS 让它看起来像您想要的,但这应该可以工作.

They key points to not there is that the table "headers" are contained in a table that has the ID "FakeHeaders". The javascript function subst() is run when the body is loaded and during the function it checks if the current page is 1 or if the current page is 3 and if it is, the FakeHeaders is set invisible. You will need to play with the margins and CSS to get it to look like you want but this Should work.

这是 wkhtmltopdf 的一个已知问题,很可能不会很快解决,请参阅 问题跟踪器中的问题 566.我认为 JavaScript 选项是唯一可用的解决方法,但如果您的输入 html、样式和页面大小/边距非常可预测,您可以尝试使用 div 或手动拆分表格 - 但请注意,这真的很烦人.

This is a known problem with wkhtmltopdf and most likely it won't be fixed any time soon, see issue 566 in the issue tracker. I see the JavaScript option as the only usable workaround, but you can try playing around with divs or manually splitting the tables if your input html, style and page sizes/margins are very predictable - but be warned, it will be really annoying.

这篇关于WKHTMLTOPDF:如何在第一页禁用页眉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆