Skip to content

Instantly share code, notes, and snippets.

@binhp
Last active April 11, 2024 11:53
Show Gist options
  • Save binhp/65b08cf9197e8aebb642532866aa5a75 to your computer and use it in GitHub Desktop.
Save binhp/65b08cf9197e8aebb642532866aa5a75 to your computer and use it in GitHub Desktop.
Netsuite-AWS-UploadBinaryFile.js
require([
"N/render",
"N/file",
"N/encode",
"/SuiteScripts/lib/ns-aws-s3"
], function(render, file, encode, AWS) {
var xmlStr =
'<?xml version="1.0"?>\n' +
'<!DOCTYPE pdf PUBLIC "-//big.faceless.org//report" "report-1.1.dtd">\n' +
'<pdf>\n<body font-size="18">\nHello World!\n</body>\n</pdf>';
var renderer = render.create();
renderer.templateContent = xmlStr;
var pdfFile = renderer.renderAsPdf();
// TODO: replace with your folder id
pdfFile.folder = 1067132;
pdfFile.name = "HELLO_WORLD.pdf";
var fileId = pdfFile.save();
// read to upload to some where ?!
var fileIdOrPath = fileId;
var fileObj = file.load({ id: fileIdOrPath });
var fileReader = fileObj.getReader();
//base64 of binary content
var fileContent = fileObj.getContents();
var rawString = encode.convert({
string: fileContent,
inputEncoding: encode.Encoding.BASE_64,
outputEncoding: encode.Encoding.UTF_8
});
var options = {
region: "ap-southeast-1",
accessKeyId: "config-me",
secretAccessKey: "config-me"
};
// sample put file
var s3 = new AWS.S3(options);
s3.putObject(
{
Bucket: "config-me",
ACL: "authenticated-read",
ContentType: "application/pdf",
Key: "share/public/hello.pdf",
Body: rawString
},
function(err, data) {
if (err) {
log.error(err, err.stack);
} else {
log.debug(data);
}
}
);
});
@natgarcia
Copy link

Didn't work for me either. The file is okay in NS but when it gets transferred to S3 is just a blank pdf.

@swa-dev
Copy link

swa-dev commented Nov 29, 2022

Has anyone gotten this to work? I am also getting a blank PDF when using this method. I am attempting to upload a PDF that was generated outside of NetSuite to S3 and am having to luck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment