Search code examples
javascriptnetsuitesuitescript

NetSuite Restlet PDF file encoding issue


My code correctly creates a file in document repository as well as attach it to a record in NetSuite. However, the file type is 'Other Binary File' when it should be a PDF. I read that for PDF we must encode in base 64 but even then it doesn't generate the PDF. Anyone see an issue?

var fileobj = nlapiCreateFile(docname, doctype, doccontent)
        fileobj.setName(docname)
        fileobj.setFolder(folderid)
        fileobj.setIsOnline(true)
        fileobj.setEncoding('UTF-8')
        var fileid = nlapiSubmitFile(fileobj)

Passed in data:

{
  "filecontent" : "test", 
  "filename" : "PO Doc Test",
  "filetype" : "PDF",
  "folderid" : 521,
  "recordtype": "purchaseorder",
  "recordid": 13832
}

Solution

  • You will have to generate your PDF using BFO. Here's a quick example that generates a PDF with the string "test" inside:

    function createPDFFile(docname, folderid)
    {
        var xml = "<?xml version=\"1.0\"?>\n<!DOCTYPE pdf PUBLIC \"-//big.faceless.org//report\" \"report-1.1.dtd\">\n<pdf>\n<body font-size=\"18\">test</body>\n</pdf>";
    
        var fileobj= nlapiXMLToPDF(xml);
    
        fileobj.setName(docname);
        fileobj.setFolder(folderid);
        fileobj.setIsOnline(true);
        fileobj.setEncoding('UTF-8');
    
        var fileid = nlapiSubmitFile(fileobj);
    }