SuiteScript渲染发票PDF:如何获取自定义PDF布局的XML模板
NetSuite Restlet 渲染发票PDF并存储问题及解决方案
问题描述
我需要通过Restlet将指定发票记录渲染为PDF并存入文件柜,但遇到了模板定位的问题:
- 当前使用的自定义PDF布局表单对应的XML模板无法找到
- PDF Templates文件夹为空,模板也未出现在「高级PDF/HTML模板」列表中
- 该表单明明显示在「交易表单PDF布局」里
想请教:有没有办法访问该模板并在「高级PDF/HTML模板」中创建副本,或者直接引用它的当前存储位置?
以下是我最初编写的函数,但不知道xmlTemplateFilePath该传入什么值:
function createPdf(fileName, invoiceId, folderId) { const f = file.create({ name: fileName + '.pdf', fileType: file.Type.PDF, folder: folderId, isOnline: true }); const xmlTemplateFile = file.load(xmlTemplateFilePath); const renderer = render.create(); renderer.templateContent = xmlTemplateFile.getContents(); renderer.addRecord('grecord', record.load({ type: record.Type.INVOICE, id: invoiceId })); const invoicePdf = renderer.renderAsPdf(); f.write(invoicePdf); f.save(); }
解决方案
已解决(采用@bknights的方案):使用render.transaction()方法可直接返回文件对象,无需手动加载模板,代码可大幅简化:
function createPdf(fileName, invoiceId, folderId) { const invoicePdfFile = render.transaction({ entityId: Number(invoiceId), printMode: render.PrintMode.PDF, inCustLocale: true }); invoicePdfFile.name = fileName + '.pdf'; invoicePdfFile.folder = folderId; invoicePdfFile.save(); }
内容的提问来源于stack exchange,提问作者Bill Gathen
相关产品推荐
相关产品推荐

