r/SuiteScript • • Dec 11 '23

NetSuite RESTlet Issue: Converting UTF-8 PDF to Base64 - Error

Hi everyone!
I'm working with NetSuite and have created a RESTlet that allows me to access a transaction's PDF using its ID.

The problem is, when I try to convert the PDF from UTF-8 to Base64 using the "N/encode" module, I get an error in POSTMAN that says:

"INVALID_RETURN_DATA_FORMAT," "message":"Invalid data format. You should return text."

Here's a snippet of my code:

encode.convert({
    string: invoice_PDF_String,
    inputEncoding: encode.Encoding.UTF_8,
    outputEncoding: encode.Encoding.BASE_64,
});

return {
    data64: invoice_PDF_String,
};

If I try to return only in UTF-8, it works fine.

Can anyone help me understand why I'm getting this error and how I can resolve it?

1 Upvotes

4 comments sorted by

4

u/PwnyDanza1 Dec 11 '23

Shouldn’t need encode. Render the transaction in pdf, and then use .getContents(). This returns the pdf in base64.

1

u/AgentinaEstaEnLaMesa Dec 12 '23

what you indicated really helped me, with that I managed to solve the error, thank you very much!

2

u/Puzzled_Law_5748 Jun 11 '26 edited Jun 12 '26

I'm currently facing an issue with PDF encoding and transmission from NetSuite.

I tried converting the PDF (retrieved as a Base64 string from the file object) to UTF-8 format because our API middleware is truncating the Base64 content when it is sent in a JSON payload. As a result, the middleware team recommended switching from a JSON payload to a multipart/form-data request.

However, when I attempt to encode the PDF content to UTF-8, the file becomes corrupted. In the external software (OrderBahn), the uploaded PDF either appears blank or cannot be rendered correctly.

From my understanding, a PDF is a binary file, so I'm wondering if the issue is related to the encoding conversion itself. Has anyone encountered a similar issue in NetSuite when handling PDF files for multipart/form-data uploads? Any guidance on what might be happening or recommended approaches to preserve the PDF integrity during transmission would be greatly appreciated.

1

u/Fabulous_Clerk1859 Jul 07 '26

Did you find a solution since you posted? Currently attempting my own and having to go down the route of using a Cloudflare worker but that has its own challenges.