Last active
April 11, 2024 11:53
-
-
Save binhp/65b08cf9197e8aebb642532866aa5a75 to your computer and use it in GitHub Desktop.
Netsuite-AWS-UploadBinaryFile.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
); | |
}); |
Didn't work for me either. The file is okay in NS but when it gets transferred to S3 is just a blank pdf.
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
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)