This topic contains 2 replies, has 0 voices, and was last updated by martytruecloud 7 years, 2 months ago.
-
AuthorPosts
-
September 13, 2017 at 4:02 pm #18296
martytruecloudHello,
I could use some assistance constructing the correct SS 2.0 code to render a PDF from a transaction leveraging an advanced template. In short, I am working with an estimate transaction where I’ve created an advanced template which I would like to consume leveraging Suitescript 2.0. The reason for this is I want to inject DocuSign dynamic content into the xml string at runtime. I think I have a pretty good foundation and the code executes successful but the PDF output is incorrect in that rather than content, the words xml.document is printed (see attached). One hack and likely part of my issue is that I am rendering to HTML 1st, so I can grab the content and alter it (not doing in this snippet). As you can see, I have to replace HTML with pdf using a regular expression since the content is wrapped in HTML tags rather than pdf tags. If there is a better way to do this, would much appreciate some direction.
Code:
/**
*@NApiVersion 2.x
*/
// This example shows how to render an invoice into a pdf file using an xml template in the file cabinet.
// Note that this example requires the Advanced PDF/HTML Templates feature
require([‘N/render’, ‘N/file’, ‘N/record’, ‘N/xml’],
function(render, file, record, xml) {var transactionFile = render.transaction({
entityId: 514,
printMode: render.PrintMode.HTML,
formId: 100
});var xmlStringContents = transactionFile.getContents().replace(/html/g, ‘pdf’); //hacky replace
log.debug(‘contents’, xmlStringContents); // contents is valid XML wrapped with PDF
var renderer = render.create();
renderer.templateContent = xmlStringContents;var customPdf = renderer.renderAsPdf();
customPdf.folder = -15;
var fileId = customPdf.save();var x = 0;
debugger;
});
Thanks for any help you can provide community friends!Marty
This is a cached copy. Click here to see the original post. -
September 13, 2017 at 9:39 pm #18297
michoelI ran your code and it worked as expected – generating a PDF version of the transaction in the SuiteScripts folder.
In any event, perhaps a cleaner way of inserting your custom content into the PDF is to use a custom field. You can use a script to populate the field with your dynamic content, and then just output it in the Advanced Template.
Another option is to store a copy of your template code in the File Cabinet, and to read that file in your script.
-
September 14, 2017 at 2:01 pm #18298
martytruecloudMichoel,
Wow, you’re right the code does work. I figured out that since I was not setting the name property for the file object NetSuite was defaulting to a value of report.pdf for the name. To add to the confusion, the file timestamp was being updated, yet the content remained the same for report.pdf in the SuiteScripts folder. Thanks for taking the time to run code! I was close to abandoning ship on this one thinking it was another SS2.0 bug!
Code:
/**
*@NApiVersion 2.x
*/
// This example shows how to render an invoice into a pdf file using an xml template in the file cabinet.
// Note that this example requires the Advanced PDF/HTML Templates feature
require([‘N/render’, ‘N/file’, ‘N/record’, ‘N/xml’],
function(render, file, record, xml) {var transactionFile = render.transaction({
entityId: 514,
printMode: render.PrintMode.HTML,
formId: 100
});var xmlStringContents = transactionFile.getContents().replace(”, ”).replace(”, ”); //hacky replace
log.debug(‘contents’, xmlStringContents); // contents is valid XML wrapped with PDF
var renderer = render.create();
renderer.templateContent = xmlStringContents;var customPdf = renderer.renderAsPdf();
customPdf.name = ‘test_1.pdf’; // need to add the name property otherwise weirdness
customPdf.folder = -15;var fileId = customPdf.save();
var id = record.attach({
record:{
type: ‘file’,
id: fileId
},
to: {
type: ‘transaction’,
id:514
}
});var x = 0;
debugger;
}); -
AuthorPosts
You must be logged in to reply to this topic.