Return nlapiPrintRecord() from a RESTlet to C#

I wanted to download a PDF of various transaction types (sales orders, invoices, etc.) from a RESTlet called from a C# application. Once I learned the “secret handshake”, it was easy. 

Here is the well-kept secret:

First, the RESTlet… It returns a Base-64 encoded string. There is no need to JSON.stringify() it. Simply send it back as-is.

RESTlet code

I’m calling my RESTlet using a System.Net.WebRequest object in the .NET Framework. Here’s what that looks like. The request uses a JSONized object to pass parameters, but the response comes back as a Base-64 encoded string.

WebRequest Code

In the code below, I’m showing how to format the PDF so the browser will know it is an attachment. I’ve hard-coded the internal ID of the sales order and am passing it as a parameter. I’m also passing the document type of “PDF.” The trick was decoding the Base-64 file to a byte array and writing it using the Response.BinaryWrite() function.


Here is what this looks like when it executes. In Internet Explorer, I get prompted to download the PDF.

Download Prompt

And then I open a nicely formatted PDF.

Sales Order

The reason I requested a PDF instead of raw HTML is because it didn’t format correctly without the required style sheets. Delivering it as a fully formatted attachment guaranteed it looked right.

Here is some code you can paste into your project.

printTransaction: function(tranid, doctype) {
var file = nlapiPrintRecord(‘TRANSACTION’, tranid, doctype.toUpperCase(), null);
return file.getValue();

Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add(“request”, “printTransaction”);
parameters.Add(“tranid”, “xxxxxx”);
parameters.Add(“doctype”, “pdf”);

OAuthCommon oAuthCommon = new OAuthCommon();

HttpWebResponse response = oAuthCommon.RESTletPOSTrequest(parameters);
Stream responseStream = response.GetResponseStream();
using (var reader = new StreamReader(responseStream))
// get the response as text
string responseString = reader.ReadToEnd();
var result = JsonConvert.DeserializeObject(responseString);

Response.ContentType = “Application/pdf”;
Response.AppendHeader(“Content-Disposition”, “attachment; filename=Test_PDF_xxxxxx.pdf”);

public HttpWebResponse RESTletPOSTrequest(Dictionary<string, object> Parameters)
const string requestMethod = “POST”;
string header = CreateOAuthHeader(requestMethod, “”);
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(baseURL);
request.ContentType = “application/json”;
request.Method = requestMethod;

string jsonData = JsonConvert.SerializeObject(Parameters);

using (var w = new StreamWriter(request.GetRequestStream()))

return (HttpWebResponse) request.GetResponse();

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s