2

NetSuite Restlet PDF file encoding issue

The above thread seems to be giving a solution to outputing a pdf with a NetSuite RESTlet. As far as I know, you cannot output a pdf from a restlet, so I'm very confused. I am using a restlet to generate a report and the information ultimately needs to output to a pdf so I was trying to see if there was a work around. I tried the answer code from the above thread and I got the expected error:"error code: INVALID_RETURN_DATA_FORMAT error message:Invalid data format. You should return TEXT."

Am I missing something? Is there a way to export xml to a pdf with a NetSuite RESTlet?

Community
  • 1
  • 1
Gus
  • 73
  • 1
  • 8

2 Answers2

3

The thread you reference discusses how to generate a PDF file in Netsuite. If you want to return a PDF from a RESTLet you will have to return it as a member of a JSON object. e.g.:

var pdfFile = genPDF(); // base this on the sample
return{
 fileName: pdfFile.getName(),
 fileContent: nlapiEncrypt(pdfFile.getValue(), 'base64')
};

And then your receiver will have to create the actual file.

Recall that RESTLets are for application-to-system communications. If you are trying to return a PDF to a browser you should probably be using a Suitelet.

If this is part of a larger app and you need the RESTLet then review this post: Save base64 string as PDF at client side with JavaScript for options to display the RESTLet response.

Community
  • 1
  • 1
bknights
  • 14,408
  • 2
  • 18
  • 31
  • Thanks! This was a Suitelet originally, but it has to process so much information that it times out before finishing the report. It was moved over to a RESTlet only for that reason. Perhaps there is a more efficient solution? I have a client script that sends some parameters to and executes the restlet, and then from the restlet I would like to send the information to a pdf. So it sounds like I need to send the data back to the client script from the restlet? – Gus Jul 05 '16 at 17:42
  • One strategy is to post the request to a custom record and trigger a scheduled script that generates the pdf from info in the custom record. The scheduled script could email the file or the page that triggers the post can monitor the queue and retrieve the result when it is complete. Or if this is a daily thing (complex pdfs of the day's orders) you could trigger this to run on a schedule and the workers could pick it up from a link in your Netsuite account when they start for the day. – bknights Jul 05 '16 at 18:02
  • This script is supposed to run only when someone presses a button to execute it. Now I have the restlet set up to create a custom record and set a field in that record to the value of the xml string that is coming from the restlet. I was thinking of having a script execute on the creation of this custom record that would then generate a pdf from the xml string. Is there a way to have the restlet wait for a response from that script that is attached to the custom record, get a link to the pdf that was generated and display that in the browser? – Gus Jul 06 '16 at 20:31
  • Not really -- As I wrote before you could have the script that calls the RESTLet kick off a polling process to look at the trigger record the RESTLet created. But first consider what is the processing that takes you to the RESTLet in the first place? If you avoid things like "nlapiLoadRecord" in a loop you can generally do quite a bit of processing within in a Suitelet's governance. Generally coalescing item ids etc and using search functions helps – bknights Jul 06 '16 at 21:13
1

Reading through that answer, it appears you'll need to encode/convert the PDF to string format before returning, so you'll need to use base64 encoding.

The NS method nlapiEncrypt(content, 'base64') seems like it might be a good place to start.

Another avenue to investigate, which I haven't tried, is to first save the PDF in the file cabinet, then to return a public link to that file. You'll need to make sure the file has the correct permissions.

TonyH
  • 1,117
  • 8
  • 18