netsuitesuitescript2.0

Find XML template file for rendering invoice PDF with SuiteScript


I'm attempting to use a restlet to render a PDF for a supplied invoice record into the File Cabinet, but I can't find the XML template for the Custom PDF Layout form we are using.

Our PDF Templates folder is empty and the template doesn't appear in Advanced PDF/HTML Templates, where I think it needs to be.

The form appears in Transaction Form PDF Layouts: is there a way to access the template and create a copy in Advanced PDF/HTML Templates, or to refer to it where it currently lives?

Here's my function, but I don't know what value to supply for xmlTemplateFilePath

  function createPdf(fileName, invoiceId, folderId) {
    const f = file.create({
      name: fileName + '.pdf',
      fileType: file.Type.PDF,
      folder: folderId,
      isOnline: true
    });

    const xmlTemplateFile = file.load(xmlTemplateFilePath);
    const renderer = render.create();
    renderer.templateContent = xmlTemplateFile.getContents();
    renderer.addRecord('grecord', record.load({
        type: record.Type.INVOICE,
        id: invoiceId
    }));
    const invoicePdf = renderer.renderAsPdf();

    f.write(invoicePdf);

    f.save();
  }

SOLVED (using @bknights solution)

render.transaction returns a file object, so I was able to simplify that, as well.

  function createPdf(fileName, invoiceId, folderId) {
    const invoicePdfFile = render.transaction({
      entityId: Number(invoiceId),
      printMode: render.PrintMode.PDF,
      inCustLocale: true
    });
    invoicePdfFile.name = fileName + '.pdf';
    invoicePdfFile.folder = folderId;

    invoicePdfFile.save();
  }

Solution

  • Rendering standard transactions doesn't work with an explicit template like that.

    You'd use render.transaction

    const invoicePdf = render.transaction({
       entityId:invoiceId,
       //formId: form linked to custom pdf template
       printMode:render.PrintMode.PDF,
       inCustLocale: true
    });
    

    If you have a specific PDF template you are trying to use you'd have to specify it in a transaction form and then use that form's id for the formId paramter.