Hi all, I have to call GET api which will return P...
# suitescript
d
Hi all, I have to call GET api which will return PDF file in binary/blob format. I then do file.create to store that file in File cabinet. The pdf file is created in File cabinet, but when i open it, it shows blank pdf. This is what the api response body is ( i truncate it because it's too long )
Copy code
%PDF-1.4
%����
1 0 obj
<<
/Type /Catalog
/Version /1.4
/Pages 2 0 R
/Names 3 0 R
>>
endobj
4 0 obj
<<
/ModDate (D:20230117152407+07'00')
/Creator (JasperReports Library version 6.10.0-dddd38218a3c404d01eecdb9d9a7636fe2d02d7a)
/CreationDate (D:20230117152407+07'00')
/Producer (iText 2.1.7 by 1T3XT)
>>
endobj
2 0 obj
>>
stream
x��Z�s�6~�ؙNg�q�[vߜ��$�#Ng:�>p	wC���������
And this is what i have in my map reduce script
Copy code
let result = https.get({
    url: url+'/api/v1/wps/'+row.wp_id+'/log-exports/'+row.pdf_id+'/download',
    headers: {
        Authorization: 'Bearer '+token_id,
        Host: url,
        Accept: '*/*',
        'Accept-Encoding': 'UTF-8'
    }
});

let encodedPdf = encode.convert({
    string: result.body,
    inputEncoding: encode.Encoding.UTF_8,
    outputEncoding: encode.Encoding.BASE_64
});

let filePdf = file.create({
    name: logPdf.fileName,
    fileType: file.Type.PDF,
    contents: encodedPdf,
    folder: 4127544
});
Need help Thanks
w
Are you sure that the body you posted is what you get from result.body? Did you post the api response from a ns-log or from postman?
Have you tried to change the accept-encoding?
d
hi @Watz, the body is not the full content that i get from the api response, but yes, i can confirm there is nothing wrong with the response.body from API, because using postman when i click Send and Download, i can get the pdf file downloaded
Copy code
Have you tried to change the accept-encoding?
how do i change this ? you mean change in NS or in the API ?
w
But Netsuite might do something with the response when it gives you the response-object.
d
i log.debug result.body and it looks the same as what i see in postman
w
In your request header you have
'Accept-Encoding': 'UTF-8'
d
oo should i remove that ?
w
Don't know. Just Trial'n'Error it until someone else comes in here with better knowledge 🙂
d
🙂
let me double check with what i have in my postman
i removed the Accept-encoding in my headers, still same result
j
I've had to deal with this on occasion but am not extremely well versed in this. However, it comes down to the fact that the N/encode module cannot convert a PDF document from octet-stream / blob / binary (whatever non base64 format you're getting) into base64. Do you have the possibility of getting the PDF in base64 from the get go? You might want to try and mess with the
Accept:
header where you indicate which format you can consume. You could try
"text/plain"
or
"text/html".
j
I’m dealing with a VERY similar issue trying to upload a PDF generated by NetSuite into Box via the Box API. The PDF has the right number of pages when it reaches Box, but every page is blank white.
(PDF is fine if stored in file cabinet, but it is a PDF generated by the render module)
b
netsuite's handling of binary content is pretty old
essentially it treats it as strings
if the content type of the response is something it recognizes as binary, it will base64 encoded the binary content into a string
if it thinks its plaintext, it will decode the binary content as utf-8 string, which is what happened here
all those � characters are encoding errors, which means your pdf is destroyed
you wont be able to use suitescript to handle the response if you cant manipulate the content type of the response to be something that netsuite recognizes as binary
ideally
application/pdf
but honestly any of the file types that are binary also work
j
Thanks for the insight, @battk
d
ic, thanks so much @battk
i guess i have to do it outside netsuite