To Generate A Pdf File Using NlapiXMLtoPDF() In Netsuite On A Button-Click

In this tip i am going to create a button on a record. Clicking the button retrieves the necessary data from the current record and creates a PDF file. The created PDF will then be shown in a seperate window by using a Suitelet.

1.   Create a suitelet with the following code to generate a pdf file using nlapiXMLToPDF in conjuction with the Big Faceless Report Generator built by Big Faceless Organization (BFO). The BFO Report Generator is a third-perty library used for converting XML to PDF documents.
The internal id of the current record that is passed as a parameter will be used to load the record.

function PrintPdf(request, response) {
try {
//retrieve the sales order Id passed to the suitelet.
var salesorderId = request.getParameter('id');
 
var salesOrderRecord = nlapiLoadRecord('salesorder', salesorderId);
 
//Retrieve some values from the sales order record which you want to display in pdf
var customer = salesOrderRecord.getFieldText('entity');
var date = salesOrderRecord.getFieldValue('trandate');
// create a table to present the results of the search
var strName = "

“; strName += “”; strName += “”; strName += “”; strName += “”; strName += “”; strName += “”; strName += ”

“; strName += “Customer: “; strName += customer; strName += “
“; strName += “Date: “; strName += date; strName += “

“; nlapiLogExecution(‘DEBUG’, ‘Table created’); // build up BFO-compliant XML using well-formed HTML var xml = “\n\n”; xml += “\n\n

Printing PDF Example

\n”; xml += “”; xml += strName; xml += “\n”; // run the BFO library to convert the xml document to a PDF var file = nlapiXMLToPDF(xml); // set content type, file name, and content-disposition response.setContentType(‘PDF’, ‘Print.pdf ‘, ‘inline’); // write response to the client response.write(file.getValue()); } catch (exception) { nlapiLogExecution(‘DEBUG’, ‘Error in PrintPdf: ‘, exception); } }

2.   Now create a Client script that will call the suitelet using nlapiResolveURL()where the internal id of the current record is passed to the suitelet. And as you can see in deployment section no function is specified because the fucntion will be called by anUserEvent Script on custom button click.

function PrintBtn() {
try {
//call the suitelet
var createdPdfUrl = nlapiResolveURL('SUITELET', 'customscript_suiteletpdf', 'customdeploy_suiteletpdf', false);
//pass the internal id of the current record
createdPdfUrl += '&id=' + nlapiGetRecordId();
 
//show the PDF file
newWindow = window.open(createdPdfUrl);
}
catch (exception) {
//alert("Error: " + exception);
}
}

3.   After this create an User Event script that will add a custom button on salesorder record.

function UserEventAddPrintBtn() {
try {
var internalId = nlapiGetRecordId();
var button = form.addButton('custpage_button', 'Print PDF',"PrintBtn();");
//set the internal id of the created Client script.
form.setScript('customscript_generatepdf');
 
}
catch (exception) {
nlapiLogExecution('DEBUG', 'Error in AddButton()', exception);
}
}

In the code as you can see the function name of the client script is specified.
The Client script is set using “setScript()“.

Now on loading any existing sales order record we will be able to see a custom button labelled “Print PDF”. And on clicking that a window opens containing the PDF file which displays the customer name and date.

150 150 Burnignorance | Where Minds Meet And Sparks Fly!