Convert liquid Pixel image to PDF from the URL and send to email when a customer placed an order

Once a customer placed an order with the Liquid Pixel customization item and we required to send the liquid pixel image with the PDF format we can use this solution to send email to any static or dynamic email using workflow Action Script

Script:

/**

 * @NApiVersion 2.x

 * @NScriptType workflowactionscript

 */

/*************************************************************************************************************************

 **************************************************************************************************************************/

define([

  "N/search",

  "N/runtime",

  "N/record",

  "N/log",

  "N/file",

  "N/https",

  "N/render",

  "N/encode",

  "N/email"

], function (search, runtime, record, log, file, https, render, encode, email) {

  function onAction(scriptContext) {

    var type = scriptContext.newRecord.type;

    var recid = scriptContext.newRecord.id;

    if (type == record.Type.SALES_ORDER) {

      var salesrec = record.load({

        type: record.Type.SALES_ORDER,

        id: recid,

      });

      var N_lines = salesrec.getLineCount({

        sublistId: 'item'

      });

      var attachmentsArray = [];

      var fileIds = []

      for (var i = 0; i < N_lines; i++) {

        var fileContent = converPDF("", salesrec.getValue({ fieldId: 'tranid' }), i + 1);

        fileIds.push(fileContent.id);

        attachmentsArray.push(fileContent.data);

      }

      if (attachmentsArray.length > 0) {

        email.send({

          author: 2496,

          recipients: 'rajesha.jagadeesh@jobinandjismi.com',

          subject: 'Liquirefire pdf Images',

          body: 'An customer has orders the liquirefire products, product sample pdf documents are atttached',

          attachments: attachmentsArray

        });

        for (var j = 0; j < fileIds.length; j++) {

          file.delete({

            id: fileIds[j]

          });

        }

      }

    }

  }

  function converPDF(url, transid, index) {

    var imageUrl ="https://iloveblvd.liquifire.com/iloveblvd set=prodID[BDY],color1[NAVY],impType[STAMP],impColor[GOLD],textline[],monoType[],graphic[anchor],rt[],seed[001]&call=url[file:main]&sink";//static LiquidPixel URL

    // Fetch the image and convert it to base64

    var response = https.get({ url: imageUrl });

    var imageBase64 = encode.convert({

      string: response.body,

      inputEncoding: encode.Encoding.BASE_64,

      outputEncoding: encode.Encoding.BASE_64,

    });

    // Create an HTML template with the base64 image

    var xmlTemplate = '<?xml version="1.0"?>';

    xmlTemplate += '<!DOCTYPE pdf PUBLIC "-//big.faceless.org//report" "report-1.1.dtd">';

    xmlTemplate += "<pdf>";

    xmlTemplate += '<body font-size="12">';

    xmlTemplate +=  '<img src="data:image/jpeg;base64,' + imageBase64 + '" width="' + "650px" + '" height="' + "650px" + '"/>';

    xmlTemplate += "</body>";

    xmlTemplate += "</pdf>";

    var pdfFile = render.create();

    pdfFile.templateContent = xmlTemplate;

    var pdfDocument = pdfFile.renderAsPdf();

    var fileObj = file.create({

      name: "" + transid + "_" +  index + ".pdf",

      fileType: file.Type.PDF,

      contents: pdfDocument.getContents(),

      folder: 67687,

    });

    var fileId = fileObj.save();

    return {id: fileId, data: fileObj};

  }

  return {

    onAction: onAction,

  };

});

Leave a comment

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