0

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
}
quarks
  • 33,478
  • 73
  • 290
  • 513
MG2016
  • 289
  • 6
  • 32

2 Answers2

3

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);
}
Hatdog
  • 296
  • 2
  • 8
  • Thanks for much for that. This seems to work. Just one final question - how should we reference our own variable inside the xml - where you have added the 'test'? – MG2016 Mar 22 '16 at 15:57
  • I figured it out. Thanks so much for your help! Saved me hours! – MG2016 Mar 22 '16 at 16:07
2

In my test of your code, changing the filename from PO Doc Test to PO Doc Test.pdf creates a file in the file cabinet with a PDF File type rather than Other Binary File.

While this creates a file that NetSuite recognizes as type PDF File, it appears that test isn't valid PDF file content, so the resulting file can't be opened by Acrobat reader.

The docs say that when trying to create binary files like PDFs, the content must be base64 encoded so you should wrap your content in nlapiEncrypt(content, 'base64').

Also, check out nlapiXMLToPDF(). This function lets you define your content in an XML string and generate a PDF from it.

Mike Robbins
  • 3,184
  • 15
  • 20
  • Thanks. Are you able to see the content? I seem to get 'Failed to Load PDF document' error. – MG2016 Mar 22 '16 at 13:34
  • Updated my answer. Content should be base64 encoded. Also, the PDF file format is more complicated that just sending text. The PDF format specification can be found on Adobe's web site here: http://www.adobe.com/devnet/pdf/pdf_reference.html. Also, here's an article about PDFs on NetSuite specifically but uses an external library: http://blog.prolecto.com/2014/01/04/framework-for-generating-custom-netsuite-pdf-files/ – Mike Robbins Mar 22 '16 at 13:37
  • I tried var doccontent = nlapiEncrypt(requestdata.filecontent, 'base64'). But still the doc doesn't open. NetSuite doc doesn't say much else regarding PDF. The same code works perfectly fine creating .txt file. – MG2016 Mar 22 '16 at 14:42