Footers And Headers

Note : we have rolled out a completely revised API in February 2014. The documentation for that is ON THE MAIN SITE. New users since february 8th can only sign up for that, whilst current API users can choose to work with the old API or switch to the new one. The documentation for the ‘old’ API features is below.

How can I generate my own header and footer content, passing variables in the url?

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

   * [page]       Replaced by the number of the pages currently being printed
   * [frompage]   Replaced by the number of the first page to be printed
   * [topage]     Replaced by the number of the last page to be printed
   * [webpage]    Replaced by the URL of the page being printed
   * [section]    Replaced by the name of the current section
   * [subsection] Replaced by the name of the current subsection
   * [date]       Replaced by the current date in system local format
   * [time]       Replaced by the current time in system local format

As an example specifying –header-right “Page [page] of [toPage]”, will result in the text “Page x of y” where x is the number of the current page and y is the number of the last page, to appear in the upper left corner in the document.

Headers and footers can also be supplied with HTML documents. As an example one could specify –header-html header.html, and use the following content in header.html:

<html>
  <head>
<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 = ['date', 'time', 'name', '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]];
    }
} 
</script>
  </head>
  <body style="border:0; margin: 0;" onload="subst()">
    <!-- 
    margin:25px 50px 75px 100px;
    top margin is 25px
    right margin is 50px
    bottom margin is 75px
    left margin is 100px
    -->
    <table style="border-bottom: 1px solid black; width: 100%;">
      <tr>
        <td style="text-align:left"><span class="name"></span> - <span class="section"></span> - <span class="date"></span></td>
        <td style="text-align:right">
          Page <span class="page"></span> of <span class="topage"></span>
        </td>
      </tr>
      <tr>
        <td style="text-align:left" colspan="2">A second line.</span>
        </td>
      </tr>
    </table>
    <br />
  </body>
</html>

As can be seen from the example, the arguments can be sent to the header/footer html documents in get fashion.

The following url shows just the header (footer) part:

http://pdfmyurl.com/test/header.html?name=fooName&section=fooSection

So, we’ve got that working, the nice thing is that most variables will be substituted automatically (page, date, time, etc….)

So we can now combine things, the following url demonstrates the working of this:

http://pdfmyurl.com/?url=http://pdfmyurl.com/test/alice.html&–margin-top=2cm&–header-html=http%3A%2F%2Fpdfmyurl.com%2Ftest%2Fheader.html%3Fname%3DAlice

Notice that the –header-html parameter has been url encoded (the / sign has become a %2F, etc.). This is complex, but is needed because the pdfmyurl server needs the complete url, and the & and ? characters will otherwise not be passed. You can read up on url encoding¬†here.

One thought on “Footers And Headers

Leave a Reply

Your email address will not be published. Required fields are marked *