Sunday, January 13, 2019

Generate a PDF file containing Headers or Footers

Here is how to add header and footer on a PDF document using nlapiXMLToPDF: (Please view this in Edit mode. The code does not render correctly if viewed in View mode/ Preview HTML)

function helloWorld()
{
var xml = "<?xml version=\"1.0\"?>\n<!DOCTYPE pdf PUBLIC \"-//big.faceless.org//report\" \"report-1.1.dtd\">";
xml += "<pdf>\n";
xml += "<head>";
xml += "<macrolist>";
xml += "<macro id=\"myheader\">";
xml += "<p align=\"left\">";
xml += "This is the Document Header";
xml += "</p>";
xml += "</macro>";
xml += "<macro id=\"myfooter\">";
xml += "<p align=\"right\">";
xml += "Page <pagenumber/> of <totalpages/>";
xml += "</p>";
xml += "</macro>";
xml += "</macrolist>";
xml += "</head>";
xml += "<body font-size=\"18\" header=\"myheader\" header-height=\"20mm\" footer=\"myfooter\" footer-height=\"20mm\">\nHello World!\n</body>\n";
xml += "</pdf>";

var file = nlapiXMLToPDF( xml );
response.setContentType('PDF','helloworld.pdf');
response.write( file.getValue() ); 
}


Notice that I have used  "macro". According to BFO's user guide, macro is used as a block of XML which can be repeated multiple times throughout the document. This "macro" needs to be used because merely using "header" and "footer" elements on the body portion of the HTML would not work.

 

No comments:

Post a Comment