Get a Free Trial

How to add headers and footers to DOCX and PDF documents?

Header

To add headers and/or footers to DOCX or PDF documents you first need to create a template, with a specified identifier. In this case report. The template can include all of the text, images and tables you want to display, along with rules for when to show the headers and footers.

Standard Template Variables

The template editor has a large selection of built in template variables, these include the following.

  • Page Number
  • PDF Title
  • URL
  • Number of Pages
  • Year
  • Month
  • Day
  • Hour
  • Minute
  • Second

Custom Template Variables

You can also specify custom template variables in your template which, can contain any plain text. It is not possible to pass HTML using custom template variables, however any /n or /r characters will be converted to line breaks.

In the example below, a custom variable is being used in the text for the header, were {number} is a custom variable.

Report number: {number}

You can then pass this template identifier to GrabzIt to add the specified header to any DOCX or PDF document. In the examples shown below the above header is displayed in each generated PDF document, for every programming language we support. However this can code can be easily adapted to add headers and footers to DOCX documents as well.

//The client should be stored somewhere and reused!
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret", client);
PDFOptions options = new PDFOptions();
options.TemplateId = "report";
options.MarginTop = 50;
options.MarginBottom = 50;
options.AddTemplateParameter("number", "12345");

grabzIt.URLToPDF("http://www.bbc.com", options);
await grabzIt.SaveAsync("http://www.example.com/Home/Index");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");
PDFOptions options = new PDFOptions();
options.setTemplateId("report");
options.setMarginTop(50);
options.setMarginBottom(50);
options.AddTemplateParameter("number", "12345");

grabzIt.URLToPDF("http://www.bbc.com", options);
grabzIt.Save("http://www.example.com/handler");
<script src="https://cdn.jsdelivr.net/npm/@grabzit/js@3.5.2/grabzit.min.js"></script>
<script>
GrabzIt("Sign in to view your Application Key").AddTemplateVariable("number", "12345").ConvertURL("http://www.bbc.com", 
{"format": "pdf", 
"mtop":50, 
"mbottom":50, 
"templateid":"report"}).Create();
</script>

To specify multiple template variables just separate each key value pair with a &.

var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");
var options = {"marginTop":50,"marginBottom":50,"templateId":"report",
"templateVariables":"number=12345"};

client.url_to_pdf("http://www.bbc.com", options);
client.save("http://www.example.com/handler", function (error, id){
    if (error != null){
        throw error;
    }
});
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret");
$options = GrabzItPDFOptions->new();
$options->marginTop(50);
$options->marginBottom(50);
$options->templateId("report");
$options->AddTemplateParameter("number","12345");

$grabzIt->URLToPDF("http://www.bbc.com", $options);
$grabzIt->Save("http://www.example.com/handler.pl");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");
$options = new \GrabzIt\GrabzItPDFOptions();
$options->setMarginTop(50);
$options->setMarginBottom(50);
$options->setTemplateId("report");
$options->AddTemplateParameter("number","12345");

$grabzIt->URLToPDF("http://www.bbc.com", $options);
$grabzIt->Save("http://www.example.com/handler.php");
grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")
options = GrabzItPDFOptions.GrabzItPDFOptions()
options.marginTop = 50
options.marginBottom = 50
options.templateId = "report"
options.AddTemplateParameter("number","12345")

grabzIt.URLToPDF("http://www.bbc.com", options)
grabzIt.Save("http://www.example.com/handler.py")

Remember when making requests please ensure all parameter values are URL encoded. Note that each POST name and value will also have to be URL encoded first. To specify multiple template variables just separate each key value pair with a &, then URL encode the tvars parameter.

https://api.grabz.it/convert?key=Sign in to view your Application Key&format=pdf&tvars=number%3D12345&mtop=50&mbottom=50&templateid=report&url=https%3A%2F%2Fwww.bbc.com%2F
grabzIt = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret")
options = GrabzIt::PDFOptions.new()
options.marginTop = 50
options.marginBottom = 50
options.templateId = "report"
options.add_template_parameter("number","12345")

grabzIt.url_to_pdf("http://www.bbc.com", options)
grabzItClient.save("http://www.example.com/handler/index")

Header or footer isn't appearing in PDF

When generating PDF documents you must ensure that there is sufficient space for your header or footer to appear. To do this specify a large enough top margin for the header to appear and a large enough bottom margin for a footer to appear. To get the margin size right may require some trial and error.