Hey All! I am working on a project to import some ...
# suitescript
r
Hey All! I am working on a project to import some files into the File Cabinet and attach them to records. I am trying to just get the basic functinality of downloading a binary file like a PDF and save it to the file Cabinet. Everything is working except when I am done I get a PDF with 32 blank pages! Also the file is about 50% larger than if I down loaded on my browser. I have seen other posts with this problem but no answers. Has anyone actually done this before? What am I missing here?
Copy code
/**
 *@NApiVersion 2.x
 *@NScriptType ScheduledScript
 */
// Pass in all modules needed
define(['N/search', 'N/record', 'N/log', 'N/runtime', 'N/file', 'N/https', 'N/encode'], function(search,record, log, runtime, file, https, encode) {
  function execute(context){
    var url = '<https://users.ece.utexas.edu/~perry/education/382v-s08/papers/raymond.pdf>';
    var response = https.get({url: url});
    log.debug({title: 'getFile response', details: {code: response.code, headers: response.headers, encodingOut: encode.Encoding.BASE_64, encodingIn: encode.Encoding.UTF_8 }});
    if(response.code == 200){
      var contents = encode.convert({string: response.body, outputEncoding: encode.Encoding.BASE_64, inputEncoding: encode.Encoding.UTF_8})
      log.debug({title: 'getFile response', details: contents});
     var fileObj = file.create({name: 'The_Cathedral_and_the_Bazaar.pdf', fileType: file.Type.PDF, contents: contents, folder: 7562116});
     fileObj.save();
    }
  }
  return {
    execute: execute
  }

});
```
b
Probably the encoding. Pdfs aren't utf-8.
Use a base 64 module from npm to do the encoding. If the file happens to be base 64 already, do not encode it again
e
I’ve in the past if it’s a one time thing simply used Netsuite’s File Cabinet to a) download all files by folder and b) upload via Advanced (ZIP Archive) and then used another M/R process to simply connect the pieces (files to records) using output searches/excel spreadsheets to prep the data for me
lo-tech solution, maybe, but it worked on large datasets
r
@battk, @ec, I realize that PDFs are not UTF-8, but my choices for the input encoding are limited to those in the encode.Encoding enum. none of the others make sense. I have tried both Hex and UTF-8. I have tried using all of the methods listed here: https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding. None of which work. as well as using this: https://github.com/engineersamuel/requirejs-base64/blob/master/base64.js none of these work. The docs say that you must base64 encode a PDF but gives no working example. I am stumped, Time to go "Low tech"