0

So, basically I am trying to get the XML template for the PDF and I plan to eventually add some additional XML in the code after getting the template working in this manner. However, when I attempt to pass a data source object to the PDF it is not working. Does anyone know the cause of this issue, and what I am doing incorrectly here?

XML template (stripped out everything but the variable in the table for testing):

<?xml version="1.0"?><!DOCTYPE pdf PUBLIC "-//big.faceless.org//report" "report-1.1.dtd">
<pdf>
<!--removed lengthy head to make code more readable-->
<body footer="nlfooter" footer-height="20pt" padding="0.5in 0.5in 0.5in 0.5in" size="Letter">
    <table class="body" style="width: 100%; margin-top: 10px;">
    <tr>
    <td>${jsonObj.terms}</td>
    </tr></table>
</body>
</pdf>

Script:

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
define(['N/error','N/render','N/file','N/record','N/log'],
/**
 * @param {error} error
 */
function(error, render, file, record, log) {

    function beforeSubmit(context) {

        log.debug('After submitting invoice, create advanced PDF detail layout', context);

        var isInvoice = context.newRecord.type == 'invoice';

        // Create advanced PDF
        if (isInvoice){
            log.audit('Creating invoice');

            renderRecordToPdfWithTemplate(context.newRecord);

        }
        else{
            error.create({
                name: 'ERROR_RECEIVED',
                message: 'Cannot create advanced PDF from this record type'
            });

            log.audit(error.name,error.message);
        }

    }

    return {
        beforeSubmit: beforeSubmit
    };

    function renderRecordToPdfWithTemplate(context) {

        var jsonObj = {
            terms: "test terms"
        };

        var templateId = '7959'; // ID of the XML
        var templateFile = file.load({id: templateId});
        var renderer = render.create();
        renderer.templateContent = templateFile.getContents();

        /*
        renderer.addRecord({
            type: record.Type.INVOICE,
            record: context
        });
        */

        renderer.addCustomDataSource({
            format: render.DataSource.OBJECT,
            alias: "jsonObj",
            data: jsonObj
        });

        log.debug('Rendering as PDF');

        var renderXmlAsString = renderer.renderAsString();
        log.debug('Added record to PDF', context);

        var invoicePdf = render.xmlToPdf({
            xmlString: renderXmlAsString
        });

        invoicePdf.name = 'Testpdf2.pdf';
        invoicePdf.folder = -15;

        try{
            var fileId = invoicePdf.save();
            log.debug('Saved PDF to file '+fileId);
        }
        catch(e){
            alert('Error saving file');
            log.debug('Error saving file');
            debugger;
        }
    }

});
J.J.
  • 1,128
  • 2
  • 23
  • 62
  • 1
    the code looks generally right. What do you mean it is not working? Do you get output but the jsonObj value is blank? With that code you'd just be seeing a blank pdf. If you add a static value instead of referencing jsonObj does that show up in the output? Very often when dealing with BFO I find my errors are not where I think they are. – bknights Nov 07 '18 at 17:49
  • 1
    I tested this exactly as you have written, except for changing the XML file ID to suit what I had when I uploaded it in my file cabinet, and it worked exactly as expected - I found the output file in the folder with ID -15 with 'test terms' in it. So as far as I can see it works as it should. – Krypton Nov 07 '18 at 20:01

1 Answers1

1

You don't need the renderer.renderAsString(); since you're already loading the XML from the file cabinet.

function renderRecordToPdfWithTemplate(context) {

    var jsonObj = {
        terms: "test terms"
    };

    var templateId = '7959'; // ID of the XML
    var templateFile = file.load({id: templateId});
    var renderer = render.create();
    renderer.templateContent = templateFile.getContents();

    renderer.addCustomDataSource({
        format: render.DataSource.OBJECT,
        alias: "jsonObj",
        data: jsonObj
    });

    log.debug('Rendering as PDF');

    var invoicePdf = renderer.renderAsPdf();
    invoicePdf.name = 'Testpdf2.pdf';
    invoicePdf.folder = -15;

    try{
        var fileId = invoicePdf.save();
        log.debug('Saved PDF to file '+fileId);
    }
    catch(e){
        alert('Error saving file');
        log.debug('Error saving file');
        debugger;
    }
}
ehcanadian
  • 1,738
  • 1
  • 15
  • 23