fpanaccia / Wkhtmltopdf.NetCore.Example-deprecated

Implementation of Wkhtmltopdf.NetCore
81 stars 33 forks source link

How to add pagination and header and footer text #1

Closed quiters89 closed 4 years ago

quiters89 commented 5 years ago

Hi, great work on this project. is there a way i can add pagination and header and footer text per page and page info like page 1 of 10? thanks

fpanaccia commented 5 years ago

Hi, thank you, not at the moment. The only thing you can do, right now, is set the page size, but i think it can be done.

vgebrev commented 4 years ago

Hi,

It looks like this functionality has since been added to the core library.

You need to cast the injected IGeneratePdf to the GeneratePdf implementation so you can set its HeaderHtml/FooterHtml properties. An example of a header html file can be found in the wkhtmltopdf documentation :

  <!DOCTYPE html>
  <html><head><script>
  function subst() {
      var vars = {};
      var query_strings_from_url = document.location.search.substring(1).split('&');
      for (var query_string in query_strings_from_url) {
          if (query_strings_from_url.hasOwnProperty(query_string)) {
              var temp_var = query_strings_from_url[query_string].split('=', 2);
              vars[temp_var[0]] = decodeURI(temp_var[1]);
          }
      }
      var css_selector_classes = ['page', 'frompage', 'topage', 'webpage', 'section', 'subsection', 'date', 'isodate', 'time', 'title', 'doctitle', 'sitepage', 'sitepages'];
      for (var css_class in css_selector_classes) {
          if (css_selector_classes.hasOwnProperty(css_class)) {
              var element = document.getElementsByClassName(css_selector_classes[css_class]);
              for (var j = 0; j < element.length; ++j) {
                  element[j].textContent = vars[css_selector_classes[css_class]];
              }
          }
      }
  }
  </script></head><body style="border:0; margin: 0;" onload="subst()">
  <table style="border-bottom: 1px solid black; width: 100%">
    <tr>
      <td class="section"></td>
      <td style="text-align:right">
        Page <span class="page"></span> of <span class="topage"></span>
      </td>
    </tr>
  </table>
  </body></html>

Hope this helps someone.

fpanaccia commented 4 years ago

Hi @quiters89 , yeah @vgebrev is right and i forgot about it, sorry. i just pushed a new version of the library which allow to set the options, so you can add or change the header, whitout the need to cast, you can look it up right here https://github.com/fpanaccia/Wkhtmltopdf.NetCore.Example/commit/63633175c7a3fcad8ea86abb218bd5339115cee6

Thanks @vgebrev for remember that!