Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use "--replace" in wkhtmltopdf

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.:)

like image 266
Veaer Avatar asked Nov 18 '14 01:11

Veaer


People also ask

How do I configure Wkhtmltopdf?

INSTRUCTIONS: Download an appropriate version of wkHTMLtoPDF library from http://wkhtmltopdf.org. If you are on Windows operating system then do install it under C:\ drive (for example c:\wkhtmltopdf). On Linux/UNIX, you can install it under /usr/local/bin and make sure wkhtmltopdf has execute permissions.

How do I use Wkhtmltopdf?

Open a command prompt window. The syntax for using the tool is fairly simple, enter the name wkhtmltopdf, followed by the URL of the web page, and the name of the PDF that you want to create, like so. Let's say you want to save a copy of a website, this is what the command will look like.

How do I create a header and footer in Wkhtmltopdf?

Footers And Headers: Headers and footers can be added to the document by the --header-* and --footer* arguments respectively. In header and footer text string supplied to e.g. --header-left, the following variables will be substituted.


1 Answers

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.

like image 147
viniciux Avatar answered Sep 22 '22 14:09

viniciux