自定义打印发票按钮出现问题,记录中的数据被拉到表格中,但是公司徽标丢失了
Having trouble with a custom print invoice button, the data from the record is being pulled to the form, however the company logo is being lost
长话短说,我在使用添加到销售订单表单的按钮时遇到了一些困难。该按钮是通过 UE 脚本在 beforeLoad 中添加的,该脚本与客户端脚本中的函数相关联,单击该脚本会使用由记录数据填充的高级 pdf/html 模板打开一个 suitelet。
通常,如果您要在销售订单上输入“编辑”,您可以 select 表格,保存它,然后当您点击打印时,一个新的 window 选项卡将打开,其中包含填充的模板和公司标志。我们有多个子公司和地点,该模板使用 if 逻辑来查找正确的徽标。按钮是一种更快更有效的解决方案,但是有一个问题....
当你点击按钮时,它会打开一个选项卡来下载套件,然后当你打开它时你会看到包含所有数据的发票,但没有标志。有时我也会收到 XML 解析器错误。以下位是正在使用的套件脚本代码:
ProFormaButtonAdd.js ( user event script)
define([],
function() {
function beforeLoad(context) {
if(context.type != context.UserEventType.VIEW){
return;
};
context.form.addButton({
id: "custpage_pfi",
label: "Pro Forma Invoice",
functionName: "onButtonClick"
});
context.form.clientScriptModulePath = "SuiteScripts/ProFormaButtonOnClick.js";
}
return {
beforeLoad: beforeLoad
};
});
ProFormaButtonOnClick.js (client script)
define(['N/url', 'N/currentRecord'],
function(url, currentRecord) {
function onButtonClick() {
var soRec = currentRecord.get();
var suiteletUrl = url.resolveScript({
scriptId: 'customscript_pro_forma_button_suitelet',
deploymentId: 'customdeploy_pro_forma_button_suitelet',
returnExternalUrl: false,
params: {
custom_id: soRec.id
},
});
window.open(suiteletUrl);
};
function pageInit(){}; // this needs to be here in order to use the client script
return {
onButtonClick: onButtonClick,
pageInit: pageInit
};
});
ProFormaButtonSuitelet.js (the suitelet script)
define(['N/render', 'N/record', 'N/xml'],
function(render, record, xml){
function onRequest(context){
var custom_id = context.request.parameters.custom_id;
var pdfFileName = "Pro Forma Invoice";
var renderer = render.create();
var content = renderer.addRecord({ // this is a concern area
templateName: 'record',
record: record.load({
type: record.Type.SALES_ORDER,
id: custom_id
})
});
renderer.setTemplateByScriptId("CUSTTMPL_131_4828307_SB1_226");
context.response.setHeader({
name: 'content-disposition',
value: 'inline; filename="' + pdfFileName + '_' + custom_id + '.pdf"'
});
context.response.writeFile(renderer.renderAsPdf()); // this is a concern area
};
return {
onRequest: onRequest
};
});
我认为可以肯定地说问题出在 suitelet 脚本部分,要么是来自记录的数据被添加到渲染器中,要么是 writeFile(renderer.renderAsPdf())。我没有找到 context.request 和 context.response 语法的 API 文档。
我尝试过对 suitelet 脚本使用其他策略,但没有成功。这是我能到达的最近的地方。另一个想法是高级 PDF/HTML 模板可能需要更新,但是,正如我之前提到的,当您更改表单并正常打印时,一切正常。
我遇到的另一个小问题是,当您点击它想要下载为“report.pdf”的按钮时,下一个是 report(1).pdf,然后是 report(2).pdf 和依此类推,但目前公司徽标是一个大问题。
非常感谢我能得到的任何反馈,你们都很棒!
更新:在 NS 2021.1 中,<img src="${companyInformation.logoUrl}">
无需像下面那样处理“&”即可工作。
如果您使用徽标的 url 作为模板中的图像源,请尝试像这样转义 url 中的“&”:
img src="${companyInformation.logoUrl?replace('&', '&', 'r')}"
长话短说,我在使用添加到销售订单表单的按钮时遇到了一些困难。该按钮是通过 UE 脚本在 beforeLoad 中添加的,该脚本与客户端脚本中的函数相关联,单击该脚本会使用由记录数据填充的高级 pdf/html 模板打开一个 suitelet。
通常,如果您要在销售订单上输入“编辑”,您可以 select 表格,保存它,然后当您点击打印时,一个新的 window 选项卡将打开,其中包含填充的模板和公司标志。我们有多个子公司和地点,该模板使用 if 逻辑来查找正确的徽标。按钮是一种更快更有效的解决方案,但是有一个问题....
当你点击按钮时,它会打开一个选项卡来下载套件,然后当你打开它时你会看到包含所有数据的发票,但没有标志。有时我也会收到 XML 解析器错误。以下位是正在使用的套件脚本代码:
ProFormaButtonAdd.js ( user event script)
define([],
function() {
function beforeLoad(context) {
if(context.type != context.UserEventType.VIEW){
return;
};
context.form.addButton({
id: "custpage_pfi",
label: "Pro Forma Invoice",
functionName: "onButtonClick"
});
context.form.clientScriptModulePath = "SuiteScripts/ProFormaButtonOnClick.js";
}
return {
beforeLoad: beforeLoad
};
});
ProFormaButtonOnClick.js (client script)
define(['N/url', 'N/currentRecord'],
function(url, currentRecord) {
function onButtonClick() {
var soRec = currentRecord.get();
var suiteletUrl = url.resolveScript({
scriptId: 'customscript_pro_forma_button_suitelet',
deploymentId: 'customdeploy_pro_forma_button_suitelet',
returnExternalUrl: false,
params: {
custom_id: soRec.id
},
});
window.open(suiteletUrl);
};
function pageInit(){}; // this needs to be here in order to use the client script
return {
onButtonClick: onButtonClick,
pageInit: pageInit
};
});
ProFormaButtonSuitelet.js (the suitelet script)
define(['N/render', 'N/record', 'N/xml'],
function(render, record, xml){
function onRequest(context){
var custom_id = context.request.parameters.custom_id;
var pdfFileName = "Pro Forma Invoice";
var renderer = render.create();
var content = renderer.addRecord({ // this is a concern area
templateName: 'record',
record: record.load({
type: record.Type.SALES_ORDER,
id: custom_id
})
});
renderer.setTemplateByScriptId("CUSTTMPL_131_4828307_SB1_226");
context.response.setHeader({
name: 'content-disposition',
value: 'inline; filename="' + pdfFileName + '_' + custom_id + '.pdf"'
});
context.response.writeFile(renderer.renderAsPdf()); // this is a concern area
};
return {
onRequest: onRequest
};
});
我认为可以肯定地说问题出在 suitelet 脚本部分,要么是来自记录的数据被添加到渲染器中,要么是 writeFile(renderer.renderAsPdf())。我没有找到 context.request 和 context.response 语法的 API 文档。
我尝试过对 suitelet 脚本使用其他策略,但没有成功。这是我能到达的最近的地方。另一个想法是高级 PDF/HTML 模板可能需要更新,但是,正如我之前提到的,当您更改表单并正常打印时,一切正常。
我遇到的另一个小问题是,当您点击它想要下载为“report.pdf”的按钮时,下一个是 report(1).pdf,然后是 report(2).pdf 和依此类推,但目前公司徽标是一个大问题。
非常感谢我能得到的任何反馈,你们都很棒!
更新:在 NS 2021.1 中,<img src="${companyInformation.logoUrl}">
无需像下面那样处理“&”即可工作。
如果您使用徽标的 url 作为模板中的图像源,请尝试像这样转义 url 中的“&”:
img src="${companyInformation.logoUrl?replace('&', '&', 'r')}"