Hi, I have the following code in my suitelet ```fu...
# suitescript
s
Hi, I have the following code in my suitelet
Copy code
function getPdf2(context){
  const pdfFileUrl = '<https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf>';
  const header = {
    'Accept': 'text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8, application/xxx',
    'Accept-Language': 'en-us'
  }
  let pdfResponse = https.get(pdfFileUrl, header);
  log.debug({title: 'getPdf2 response header', details: JSON.stringify(pdfResponse.headers)});
  log.debug({title: 'getPdf2 response body', details: pdfResponse.body});
  log.debug({title: 'getPdf2 response content-length', details: pdfResponse.headers['Content-Length']});
  const pdfFile2 = file.create({
    name: 'dummy.pdf',
    fileType: file.Type.PDF,
    contents: encode.convert({
      string: pdfResponse.body,
      inputEncoding: encode.Encoding.UTF_8,
      outputEncoding: encode.Encoding.BASE_64
    })
  });
  context.response.writeFile({file: pdfFile2, isInline: true});
}
what I am trying to achieve is to get pdf from another url and display the file content in the suitelet. The result shows empty file content. It appears that I am doing something wrong in the encode.convert section. Does anyone have experience with it?
k
Are you sure inputEncoding: encode.Encoding. is UTF I thought netsuite always returns base64?
s
I am not sure, but the content of the file is not from NetSuite. it’s from pdfResponse.body. I do tried use inputEncoding to BASE_64 and it exception out
b
netsuite interprets the response body as utf-8 when it doesnt recognize the Content-Type
The encoding errors will usually destroy any binary file
You will probably need to start looking at alternatives if the server you are working with can't set the Content-Type header to exactly
application/pdf
A content type of
application/pdf
should make the body base64, which you can save directly into the file cabinet
s
The response content-type is application/pdf, what I am struggling with is how did I get the file from response?
b
If the response body is in base 64, then you simply use pdfResponse.body as your contents
If it is not, you need to consider alternatives
s
How should I determine if response body is in base 64? Assign content to response.body and see if it throws error?
b
you can make a reasonable guess from the contents of the body
if it only contains the characters used in base64, then its probably in base64