Apurva16
ServiceNow Employee
ServiceNow Employee

The document template is a powerful feature of the ServiceNow platform, it allows you to create HTML and PDF document templates to generate standard letters or documents. You can automate and simplify the process of filling, signing, and reviewing a document online.

Some of the major features are:

  • Document blocks: have parts of documents appear based on when specific conditions are satisfied.
  • Document variables: easily populate dynamic data from ServiceNow
  • Document Template Script: Powerful tool to fetch/modify variables.

However, there are some limitations in the Out of the Box (OOTB) functionality one of the major ones encountered was that the headers and footers are static and are attached to the document template.

Problem:

The customer has 10 legal entities and needs a contract/documents generated for each of them. The body of the documents is the same or can be made the same with the usage of blocks and variables. However, the header and footers of each Legal entity are different.

Since the header and footers are static and are by the document templates, a document template needs to be created per legal entity and maintained. This can cause considerable development and maintenance effort.

The problem increases exponentially if the customer has many legal entities.

find_real_file.png

find_real_file.png

Solution:

The HTML documents can have CSS scripts in them. We can use it to our advantage to introduce the dynamic header and footer functionality.

In the HTML code of the document introduce the below at the beginning of the code:

“<style>

@page {

  @top-center {

    content: element(pageHeader);

  }

}

#pageHeader{

  position: running(pageHeader);

}

</style>

<style>

 @page {

  @bottom-center {

    content: element(pageFooter);

  }

}

#pageFooter{

  position: running(pageFooter);

}

</style>

<style type="text/css">

    @page {

        @bottom-right {

            content: counter(page) " of " counter(pages);

        }

    }

</style>

<div id="pageHeader">${template_script:header}</div>
<div id="pageFooter">${template_script:footer}</div>”

<<actual document text from here on>>

Let’s breakdown the above code for a bit of better understanding.

““<style>

@page {

  @top-center {

    content: element(pageHeader);

  }

}

#pageHeader{

  position: running(pageHeader);

}”

The above tells the system to have the header in the top center position. You can also change it to top-left or top-right. The running(pageHeader) ensures that the header is present on each page.

The same logic applies to the footer code.

The code:

“<div id="pageHeader">${template_script:header}</div>
<div id="pageFooter">${template_script:footer}</div>”

This is how we call the header and footer values. Instead of the script if you call a static text or image then the document template will have just that, however, using script we can fetch the appropriate header and footer. The logic for the script would be:

if (target,subject_person.Legal_Entity == “Legal entity A”) {

return “Legal entity header 1.png”;

}

elseif (target.subject_person.Legal_Entity == “Legal entity B”) {

return “Legal entity header 2.png”;

}

… and so on…

Same logic for the footer template script.

Using the dynamic header and footer you can have the same document for the various legal entities and the system would populate the document with the right Header and footer and the appropriate variables. This would significantly reduce the development and maintenance time.

Your document template body would end up looking something like this:

find_real_file.png

Things to note:

  • The image of the header and footers should be uploaded in ServiceNow. The URL might take a while to be figured out, in my experience, it might be better to put the full URL and the image config such as height and width.

Example:

return '<img style="align: baseline;" title="" src="https://<your-instance-url-here>/headerAImage.png" alt="" width="227" height="25" align="baseline" border="" hspace="" vspace="" />';

  • Do not upload the header/footer/page number directly in the document template where you are adding it currently as it will conflict with the code. Leave it blank/default settings.find_real_file.png
  • If you are on San Diego version or later you might have to change the property; snc.pdfgenerator.html2pdf.api.version to 1. Please note that the Table of content will also not work after you switch this property.find_real_file.png
  • The logic for page numbers is also provided in the code.
  • If you need to edit the document template, ensure that you are clicking on an actual text inside the document template and then clicking on the “<>” button. Failure to do so will introduce a <span> tag on the header and will prevent it from working.
  • The preview view will look a bit different so after you click preview, go for the preview as PDF.
  • The logic of this article is only valid for HTML document type.

 

I hope this article helps you in creating the document templates. Please do feel free to provide feedback/questions.

 

 

Comments
Prabneet Kaur
Tera Contributor

Hi, 

Thank you for above, this was really helpful!. In my use case I need both the pageHeader content and page number content to be on the top-right corner. Can you help with the code? For ex. I need the name of the user and below that I need the Page number. Can you please help. 

Apurva16
ServiceNow Employee
ServiceNow Employee

Hi,

Please try below. You might have to tweak the code a bit.

You can replace template_script variable with your header and footers.

Hope this helps.

 

<style type="text/css">
@page {
  @top-right {
    content: element(pageHeader);
  }
  @bottom-center {
    content: element(pageFooter);
  }
 margin-top: 175px;
}
 #pageHeader{
  position: running(pageHeader);
}
 #pageFooter{
 position: running(pageFooter);
}
 #pageHeader::after {
 content: '\A' counter(page) ' / ' counter(pages);
 font-family: tahoma, arial, helvetica, sans-serif;
 font-size: 8pt;
}
</style>
<div id="pageHeader">${template_script:header_script}</div>
<div id="pageFooter">${template_script:footer_script}</div>
Umarta Das1
Tera Contributor

Hi Apurva,

Thank you for this solution. Once implemented, it is going to help my current client a lot.

However, when I was trying to only implement the dynamic footer, the content is just getting populated where the template script is added. I've also altered the version of the property to 1

Here are the snips:

find_real_file.png

 

find_real_file.png

Instead of an image, I'm trying to populate text.

find_real_file.png

find_real_file.png

Please let me know where I'm going wrong.

Thanks in advance,
Umarta

 

Apurva16
ServiceNow Employee
ServiceNow Employee

Hi @Umarta Das ,

could you type a normal text instead of the template script in the DIV for the footer and try again?
If that works, the issue could be with the script.

also after the div for footer, can you add a static text before you call the block and try again?
You code does look fine, but maybe it would be helpful if you can provide a bigger snipped of the code on the document template.

Thank you.

raul4
Tera Explorer

Hi @Apurva16,

 

Followed to the instructions from first post and although the footer is added when previewing template, when it is generated to .pdf. 

 

Client is on San Diego patch7.

 

Apurva16
ServiceNow Employee
ServiceNow Employee

@raul4 I am not sure if I followed your question. Are you suggesting that the footer comes up when you preview but not when you click generate? Did you check the system properties that I mentioned in the article?

Harsha38
Tera Guru

Hi @Apurva16  Adding footer to each page of a document is not working for me. I tried the above code. If we have two pages it adds footer at the bottom of the last page. My requirement is to have footer on each page. Please help me on this. Its a urgent requirement. Thank you

Apurva16
ServiceNow Employee
ServiceNow Employee

Hi @Harsha38 ,

 

The code should add a running footer on every page. Could you please replace the image with just a text and try again?

 

Please paste the code here if it still doesn't work.

Sangeetha Naga1
Tera Contributor

Hi @Apurva16 

I have an existing document template and added few attachments to the template. Now while i'm trying to delete 2 of the attachments (images) , I'm unable to delete them as there is no option to delete. Is there a way to delete the attachments from the document template? Please suggest!

 

Thanks in Advance!!

Sangeetha N

Sangeetha Naga1
Tera Contributor

PFA

attach1.PNG

Apurva16
ServiceNow Employee
ServiceNow Employee

@Sangeetha Naga1 ,

 

Is the "Manage Attachments" clickable? usually it is and you can select and delete the attachment from there.

Surabhi7
Tera Contributor

Hi Apurva,

 

We did try this code for our use cases,  we noticed when the document is previewed from backend i.e click on Preview button in document template , the HTML is rendered line by line and the footer shows at the bottom, But when we generated the document , it generates the 2 pages document and the footer is missing in 1st page.

 

We are generating the document via case creation from record producer.

 

Could you please advice if we need to do anything specific when PDF is generated from record producer

 

TIA

Surabhi

Nagashree5
Tera Contributor

Hi @Apurva16,

 

Thank you so much for the article. It was really helpful.

 

I have created the HTML template in sn_doc_html_template table on a custom RITM template. How can create the PDF from this every time a custom RITM for a specific form get's submitted. Can you please suggest. I have posted a question as well on this - Document Templates for RITM's - ServiceNow Community

Can we call this template from the workflow and generate the PDF? Please help!

 

Thanks in Advance.

Abhinandan Bhan
Tera Expert

Thanks for this useful information. Is there a way to have same header and footer on multiple pages automatically?

 

Don Dom
Tera Contributor

Hello

Margins for the whole A4 page are set 36 points.

I need to move this Header banner to the maximum LEFT - only banner. How to do it please?

 

DonDom_1-1702290720912.png

 

Apurva16
ServiceNow Employee
ServiceNow Employee

@Surabhi7,

You probably had a mistake in the code or you called the footer incorrectly. Could you please check and let me know if the issue persists.

Apurva16
ServiceNow Employee
ServiceNow Employee

Hi @Nagashree5 ,

You could try to have a business rule or a flow. There is an action available in the flow designer to generate the document. There is also an API available that you can call in your BR to do the same. I hope this helps.

Apurva16
ServiceNow Employee
ServiceNow Employee

@Abhinandan Bhan ,

Yes of course, we are having the same footer in every page. You can use similar logic to have the same header in every page.

Abhinandan Bhan
Tera Expert

@Apurva16 I am facing same issue as @Harsha38 . If any of you have solution to it then please let me know. Thanks.

 

Problem:  Adding footer to each page of a document is not working for me. I tried the above code. If we have two pages it adds footer at the bottom of the last page. My requirement is to have footer on each page. Please help me on this. Its a urgent requirement. Thank you

Umarta Das1
Tera Contributor

Hi @Apurva16,

 

I had used the piece of code you have mentioned for a different client where I was populating data from a different table instead of an image. It worked fine. Thank you!

 

For my current client, the ask is to print the page number in the following format: counter(page) " of " counter(pages)

 

It is working fine till a point where the counter is increasing but the total number of pages is not. Do you know a way out?

 

Adding snips:

UmartaDas1_0-1710404289967.png

 

UmartaDas1_1-1710404349567.png

 

gvk
Tera Contributor

Hi,

 

Helpful article 👏.

 

I tried this solution to dynamically add footer image. The generated document had footer but the document also showing the name of the image under the footer. Is there any way to show only image?

OmkarC
Tera Guru

Hi @Apurva16 ,

Thank you for such a informative article. It is really helpful.

Thanks & Regards,
Omkar

Apurva16
ServiceNow Employee
ServiceNow Employee

@gvk I did not face this issue at all. It might be something in the CSS code or maybe a sys property that you have in the instance.

Version history
Last update:
‎08-29-2022 06:42 AM
Updated by: