简体   繁体   中英

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
}

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);
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM