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);
}
}
);
});
@jjmartin
Copy link

Thanks for helping on this. When I use this and save the contents as UTF-8, it saves as a correct PDF (acrobat doesn't freak out when it loads it) but its completely blank
I've tried getting the contents after just doing a renderAsPDF from template, and from saving the file as a PDF and then loading it from the file cabinet before going to S3 and still gets blank

@binhp
Copy link
Author

binhp commented May 20, 2019

hi @jjmartin , I just update to use renderAsPdf to print pdf "hello world", It should work with dynamic data too.
The issue of blank pdf could be from incorrect pdf template or reference missing data/variable. I could no re-produce because I do not have these data or template, but feel free to simplify your template to see if it show some thing.

(I did take a look at your gist https://gist.github.com/jjmartin/112daec3f928eb367253aaf896688dae)

(screenshot above output)

Screen Shot 2019-05-20 at 22 14 28

@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