How to use "--replace" in wkhtmltopdf
Asked Answered
G

3

7

I saw all that content, didn't see about the content of the way to use '--replace'. How to use "--replace" in wkhtmltopdf. Please give me an example,Thanks.:)

Glynnis answered 18/11, 2014 at 1:54 Comment(1)
Did you ever figure this one out. If you did, you should post the answer.Gordie
B
3

Lets say you have a footer my_footer.html which contains the following:

<!DOCTYPE html>
<html>
  <head>
  <meta charset="utf-8">
  <script>
      function substitutePdfVariables() {

          function getParameterByName(name) {
              var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
              return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
          }

          function substitute(name) {
              var value = getParameterByName(name);
              var elements = document.getElementsByClassName(name);

              for (var i = 0; elements && i < elements.length; i++) {
                  elements[i].textContent = value;
              }
          }

          ['username'/*, 'topage', 'page',.. etc and any variable you would want to replace */]
              .forEach(function(param) {
                  substitute(param);
              });
      }
  </script>
  </head>
  <body onload="substitutePdfVariables()">
    <span class="username"></span>
  </body>
</html>

You can replace "username" with some value:

wkhtmltopdf my_super_html.html --footer-html my_footer.html --replace "username" "Veaer"

Notice that I used javascript to replace the named variable with my value.

Beverlee answered 28/7, 2016 at 0:2 Comment(2)
Is it possible to have keys and values defined in the javascript as associative array, not as parameter to wkhtmltopdf? I use variables all over a markdown document.Converting from .md to .html works with pandoc including the js as header file in the pandoc call.But then the conversion from html to pdf with these variables does not convert the variables anymore, they remain as keys.Dumas
@Dumas I'm not sure if that's possible, unfortunatelyBeverlee
S
1

I did some adjustments to @viniciux answer which is actually working so here is the improvements in footer.html

<!DOCTYPE html>
<html>
  <head>
  <meta charset="utf-8">
  <script charset="utf-8"> 
    function replaceParams() {
      var url = window.location.href        
        .replace(/#$/, ""); // Remove last # if exist
      // default params
      //['page', 'section','sitepage','title','subsection','frompage','subsubsection','isodate','topage','doctitle','sitepages','webpage','time','date'];
      var params = (url.split("?")[1] || "").split("&");
      for (var i = 0; i < params.length; i++) {
          var param = params[i].split("=");
          var key = param[0];
          var value = param[1] || '';
          var regex = new RegExp('{' + key + '}', 'g');
          document.body.innerText = document.body.innerText.replace(regex, value);
      }      
    }
  </script>
  </head>
  <body onload="replaceParams()">
    name: {username}
    age: {age}
    url: {url}
    page {page}
  </body>
</html>

and then you can add multiple parameters to replace dynamically

wkhtmltopdf page.html --footer-html footer.html --replace "username" "Veaer" --replace "age" "20" --replace "url" "google.com"

notice that page will be current pdf page and will be included by default in the parameters list

Sigrid answered 28/5, 2020 at 23:1 Comment(1)
This solution clear the stylesheet for meFoss
F
1

A mixed solution from @dpineda and @viniciux. This solution doesn't break the stylesheet

<!DOCTYPE html>
<html>
  <head>
  <meta charset="utf-8">
        <script charset="utf-8"> 
            // For WKHTMLTOPDF Substitutions
            window.onload = function()
            {
                function substitute(key, value)
                {
                    var elements = document.getElementsByClassName(key);
                    for (var i = 0; elements && i < elements.length; i++)
                    {
                        elements[i].textContent = value;
                    }
                }

                var url = window.location.href.replace(/#$/, ""); // Remove last # if exist
                // default params
                //['page', 'section','sitepage','title','subsection','frompage','subsubsection','isodate','topage','doctitle','sitepages','webpage','time','date'];
                var params = (url.split("?")[1] || "").split("&");
                for (var i = 0; i < params.length; i++)
                {
                    var param = params[i].split("=");
                    var key = param[0];
                    var value = param[1] || '';
                    var regex = new RegExp('{' + key + '}', 'g');
                    substitute(key, value);
                }
            }
        </script>
  </head>
  <body onload="substitutePdfVariables()">
    <span class="username"></span>
  </body>
</html>
Foss answered 22/1, 2021 at 14:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.