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

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

问题描述

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

解决方案

您是否尝试过JS解决方案?其实并不复杂。我只是做了一个长的HTML文件的测试,其中包含一个表被拆分成许多不同的页面,我使用这个头文件从第1页和第3页删除了页眉:

 < HTML> 
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8/>
< script>
函数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
if(vars ['page'] == 1){//如果page为1,则将FakeHeaders设置为none
document.getElementById(FakeHeaders)。style.display = '没有';


if(vars ['page'] == 3){//如果页面为3,则将FakeHeaders设置为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>
您的真棒表格列标题1< / th>
< th>列2< / th>
< th style =text-align:right>
Page< span class =page>< / span> /< span class =topage>< / span>
< / th>
< / tr>
< / table>
< / body>
< / html>

它们的关键点不在于表格标题包含在具有IDFakeHeaders。 javascript函数subst()在主体加载时运行,并在函数检查当前页面是否为1或当前页面是否为3,如果是,FakeHeaders设置为不可见。您需要使用边距和CSS来让它看起来像你想要的,但这应该工作。



这是wkhtmltopdf的一个已知问题,很可能它赢了我们很快就会解决,请参阅 issue 566在问题跟踪器中。我将JavaScript选项看作唯一可用的解决方法,但是如果输入html,样式和页面大小/页边空白非常可预测,您可以尝试使用div或手动分割表格,但要注意,这会非常烦人。 / p>

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?

解决方案

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>

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.

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天全站免登陆