在 PDF 模板中运行保存的搜索和渲染的脚本

问题描述 投票:0回答:1

我有一个用例,我们需要创建一个保存的搜索和一个高级 PDF 模板来配合它。我需要一个脚本来运行搜索并呈现我制作的自定义 PDF 模板。似乎这一切都是有可能的。我有它,所以保存的搜索可以正常加载。但我无法让搜索来填写模板。错误提示:“缺少生成 PDF 所需的参数”。有人可以帮助我让它朝正确的方向发展吗?

剧本:

/**
 * @NApiVersion 2.x
 * @NScriptType ScheduledScript
 */
require(['N/search', 'N/render', 'N/email', 'N/file'], function(search, render, email, file) {
    function runSearchAndFetchResult() {
        try {

            var mySearch = search.load({
                id: 2825
            });
 
            var searchResult = mySearch.run().getRange({
                start: 0,
                end: 10
            });
            log.debug('searchResult', searchResult);
            var renderer = render.create();
            renderer.templateContent = renderer.setTemplateByScriptId("custtmpl_nscs_bin_label");

            renderer.templateContent = renderer.renderAsString();;

            renderer.addSearchResults({
                templateName: 'results',
                searchResult: searchResult
            });
            
            var newfile = renderer.renderAsPdf();
            
            newfile.name = 'Testpdf2.pdf';
            newfile.folder = 2754;
            
            // Save the file
            newfile.save();

        } catch (e) {
            log.error({
                title: 'Error',
                details: 'An error occurred: ' + e.message
            });
        }
    }

    runSearchAndFetchResult();
});

高级 PDF 模板

<?xml version="1.0"?><!DOCTYPE pdf PUBLIC "-//big.faceless.org//report" "report-1.1.dtd">
<pdf>
<head>
    <link name="NotoSans" type="font" subtype="truetype" src="${nsfont.NotoSans_Regular}" src-bold="${nsfont.NotoSans_Bold}" src-italic="${nsfont.NotoSans_Italic}" src-bolditalic="${nsfont.NotoSans_BoldItalic}" bytes="2" />
    <#if .locale == "zh_CN">
        <link name="NotoSansCJKsc" type="font" subtype="opentype" src="${nsfont.NotoSansCJKsc_Regular}" src-bold="${nsfont.NotoSansCJKsc_Bold}" bytes="2" />
    <#elseif .locale == "zh_TW">
        <link name="NotoSansCJKtc" type="font" subtype="opentype" src="${nsfont.NotoSansCJKtc_Regular}" src-bold="${nsfont.NotoSansCJKtc_Bold}" bytes="2" />
    <#elseif .locale == "ja_JP">
        <link name="NotoSansCJKjp" type="font" subtype="opentype" src="${nsfont.NotoSansCJKjp_Regular}" src-bold="${nsfont.NotoSansCJKjp_Bold}" bytes="2" />
    <#elseif .locale == "ko_KR">
        <link name="NotoSansCJKkr" type="font" subtype="opentype" src="${nsfont.NotoSansCJKkr_Regular}" src-bold="${nsfont.NotoSansCJKkr_Bold}" bytes="2" />
    <#elseif .locale == "th_TH">
        <link name="NotoSansThai" type="font" subtype="opentype" src="${nsfont.NotoSansThai_Regular}" src-bold="${nsfont.NotoSansThai_Bold}" bytes="2" />
    </#if>
    <style type="text/css">table { font-size: 9pt; table-layout: fixed; width: 100%; }
th { font-weight: bold; font-size: 8pt; vertical-align: middle; padding: 5px 6px 3px; background-color: #e3e3e3; color: #333333; padding-bottom: 10px; padding-top: 10px; }
td { padding: 4px 6px; }
b { font-weight: bold; color: #333335; }
</style>
</head>
<body margin-left="-35" margin-top="-35" size="2.5in x 1in">
  <#list results as result>
       <table float="left">
    <tr>
    <td colspan="3" style='font-weight:bold; font-size: 14pt;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;${result.binnumber}</td>
</tr>
        
         
    <#if result?has_next>
    <tr>
        <td colspan="3"><barcode codetype="code128" showtext="false" value="${result.binnumber}"></barcode></td>
    </tr></table>
        <pbr />
    <#else>
         <tr>
        <td colspan="3"><barcode codetype="code128" showtext="false" value="${result.binnumber}"></barcode></td>
    </tr></table>
    </#if>

    </#list>
</body>
</pdf>

The Advance PDF Template setup

netsuite suitescript suitescript2.0
1个回答
0
投票

当您调用

renderer.setTemplateByScriptId()
时,该函数会更新
renderer.templateContent
的值 - 您不需要分配它。

然后您调用

renderer.renderAsString()
并将其也分配给
renderer.templateContent
renderer.renderAsString()
方法返回一个 xml 字符串,稍后可以在调用
render.xmlToPdf()
时使用该字符串,并且在您的示例中不需要(请注意,
xmlToPdf()
render
模块的方法,而显示的其他方法是
TemplateRenderer
对象的方法。)

要将这些注释付诸实践,请替换以下行:

renderer.templateContent = renderer.setTemplateByScriptId("custtmpl_nscs_bin_label");

renderer.templateContent = renderer.renderAsString();;

与:

renderer.setTemplateByScriptId({
    scriptId: "custtmpl_nscs_bin_label"
});

我还没有时间对此示例进行测试,因此可能存在我忽略的其他错误,但希望这有助于您走上正确的道路。

© www.soinside.com 2019 - 2024. All rights reserved.