Does anyone have any resources for a sample script...
# general
u
Does anyone have any resources for a sample script that prints out a transaction from an Advanced PDF/HTML template from a suitelet? I'm currently trying to generate a PDF for a Price Quote from a Suitelet. I've managed to pass over the transaction's record ID to the Suitelet, but I've hit a road block as all examples I can find regarding printing using a Suitelet involves using XML templates and files. Would appreciate any help, thanks.
b
xml template and files are basically the things you need to generate pdfs
you can use TemplateRenderer.setTemplateByScriptId or TemplateRenderer.setTemplateById instead of loading a template file if you create a advanced pdf template record in netsuite
you then use ServerResponse.writeFile(options) with the file you generated to return the file to the browser
u
Thanks for the response. I've actually managed to generate a PDF file already using the transaction, but the problem is getting the PDF to follow a template I made in Advanced PDF/HTML templates in Netsuite. I tried using TemplateRenderer.setTemplateByScriptId as you mentioned, but the PDF it generates doesn't seem to follow it. For context, here's the Suitelet code:
Copy code
/**
* @NApiVersion 2.x
* @NScriptType Suitelet
*/
define(['N/render','N/record'], function(render,record) {
	var exports = {};

	function onRequest(context) {
        var id = parseInt(context.request.parameters.custom_id,10);
        var renderer = render.create();
        var transactionFile = render.transaction({
          entityId: id,
          printMode: render.PrintMode.PDF
        });
      	renderer.setTemplateByScriptId({
          scriptId:'CUSTTMPL_PFI'
        });
		context.response.writeFile(transactionFile);
	}
	exports.onRequest = onRequest;
	return exports;
});
This successfully generates the PDF, but doesn't follow the template I assigned it to.
b
render.transaction renders the pdf that is specified on the form
you use the file that it rendered when writing to your response
follow the example, set the template, add the record, then render using TemplateRenderer.renderAsPdf
u
From my understanding of how you're explaining it, render.transaction renders the transaction onto a PDF layout that's configured to the form it's using. This is fine, but my desired output is that I want it to use a separate PDF layout than the form is currently using. I'm not quite sure what you mean when you said I should "use the file that it rendered when writing" my response. Can I ask you to elaborate? I've also tried following your suggestion, following the example as closely as possible, with the only significant changes being my removal of the N/file module and replacing the loading of a template file with TemplateRenderer.setTemplateByScriptId.
Copy code
/**
 *@NApiVersion 2.x
 *@NScriptType Suitelet
 */
define(['N/render', 'N/record'], function(render, record) {
	var exports = {};

    function renderRecordToPdfWithTemplate(context) {
        var renderer = render.create();
        renderer.setTemplateByScriptId({
          scriptId:'CUSTTMPL_PFI'
        });
        renderer.addRecord('record', record.load({
          type: record.Type.ESTIMATE,
          id: parseInt(context.request.parameters.custom_id,10)
        }));
        var invoicePdf = renderer.renderAsPdf();
    }

  exports.onRequest = renderRecordToPdfWithTemplate;
  return exports;
});
This one won't even generate a PDF. I feel like I'm missing something on the template binding part from earlier, but I just can't quite figure it out.
b
Copy code
function onRequest(context) {
  var id = parseInt(context.request.parameters.custom_id, 10);
  var renderer = render.create();
  var transactionFile = render.transaction({
    entityId: id,
    printMode: render.PrintMode.PDF,
  });
  renderer.setTemplateByScriptId({
    scriptId: "CUSTTMPL_PFI",
  });
  context.response.writeFile(transactionFile);
}
is equivalent to
Copy code
function onRequest(context) {
  var id = parseInt(context.request.parameters.custom_id, 10);
  var transactionFile = render.transaction({
    entityId: id,
    printMode: render.PrintMode.PDF,
  });
  context.response.writeFile(transactionFile);
}
you dont do anything with
renderer
except set its template
you use the file generated by
render
and write that in your response
at no point did you use
renderer
in your output
your new code generates
invoicePdf
, but does nothing with it
at the very least, you want to write the file to the response