Generating PDF using renderAsPdf()

The renderAsPdf() method in the N/render module of SuiteScript 2.x is used to render content into a PDF document. This method is particularly useful for creating PDF reports, invoices, or any other documents that need to be dynamically generated based on templates and data. By using this method, you can create custom PDFs that include dynamic data, save them to the File Cabinet, and serve them directly to users.

The renderAsPdf() method is called on a renderer object that has been configured with a template and data.

For example:

var renderer = render.create();

      renderer.templateContent = templateFile.getContents();

      // Add dynamic data to the template

      renderer.addCustomDataSource({

        format: render.DataSource.OBJECT,

        alias: ‘data’,

        data: {

          name: ‘John Doe’ // This data will be used in the template

        }

      });

      // Render the PDF

      var pdfFile = renderer.renderAsPdf();

      // Optional: Save the PDF to the File Cabinet

      pdfFile.name = ‘generatedPdf.pdf’;

      pdfFile.folder = 123; // Replace with your target folder ID

      var fileId = pdfFile.save();

Leave a comment

Your email address will not be published. Required fields are marked *