WKHTMLTOPDF: how to disable the title on the first page

wkhtml does not repeat the elements of the table "th" on each page, as it should. So I thought that you could just use the -header-html parameter and manually add the table headers. But I do not want them on the first page, since there are already table headers, as well as some other materials on the first page ... I found some kind of JS solution, but for me it is too complicated, since I know only the very basics of JS .. Any ideas?

+6
source share
3 answers

Have you tried the JS solution? This is actually not so difficult. I just did a test with a long html file that contained a table divided into several different pages, and I was able to remove the headers from pages 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 indicate that the header tables are not contained in the table with the identifier FakeHeaders. The javascript subst () function is run when the body is loaded, and during the function it checks whether the current page is 1, or if the current page is 3, and if so, FakeHeaders is set invisible. You will need to play with the fields and CSS to make them look the way you want, but this should work.

This is a known issue with wkhtmltopdf and most likely it will not be fixed in the near future, see question 566 in the tracker release . I see the JavaScript option as the only workaround, but you can try playing with a div or manually breaking tables if your input html, style and page / margin sizes are very predictable - but be careful, this will be very unpleasant.

+14
source

If you can share the first page as a separate html, you can do this using the "cover" in WKHTMLTOPDF.

 PDFKit.new(url, :header_html => header_url, :cover => cover_url). 
+2
source
 <script type="text/javascript"> var pdfInfo = {}; var x = document.location.search.substring(1).split('&'); for (var i in x) { var z = x[i].split('=',2); pdfInfo[z[0]] = unescape(z[1]); } function getPdfInfo() { var page = pdfInfo.page || 1; if(page != 1) { document.getElementById('pHeader').style.display = 'none'; } } getPdfInfo(); </script> 
0
source

Source: https://habr.com/ru/post/922803/


All Articles