为什么此高级PDF模板没有从传递给它的对象中填充术语?

时间:2018-11-07 12:42:17

标签: netsuite suitescript2.0 bfo

因此,基本上,我正在尝试获取PDF的XML模板,并且我打算在使模板以这种方式工作后最终在代码中添加一些其他XML。但是,当我尝试将数据源对象传递给PDF时,它不起作用。有谁知道这个问题的原因,以及我在这里做错了什么?

XML模板(剔除表中变量以进行测试的所有内容):

<?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>

脚本:

/**
 * @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;
        }
    }

});

1 个答案:

答案 0 :(得分:1)

您不需要renderer.renderAsString();,因为您已经从文件柜中加载了XML。

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;
    }
}