如何在单击按钮时显示自定义PDF模板

时间:2016-06-27 06:49:51

标签: pdf netsuite suitescript

我想在点击销售订单中的按钮时在新窗口中显示PDF模板。我使用用户事件脚本在销售订单流程中创建了按钮。之后,我无法继续下去。单击销售订单时,可以在新窗口中显示自定义PDF模板吗?

我的代码:

USER EVENT SCRIPT:

     // creating button in user event script before load event in view mode
        unction userEventBeforeLoad(type, form, request){  


    if(type == 'view'){
         var internalId = nlapiGetRecordId();

            if (internalId != null) {
                var createPdfUrl = nlapiResolveURL('SUITELET', 'customscript_back0rdered_itm_pdf', 'customdeploy_backord_itm_pdf_dep', false);
                createPdfUrl += '&id=' + internalId;

                //---add a button and call suitelet on click that button and it will open a new window
                var addButton = form.addButton('custpage_printpdf', 'Print PDF', "window.open('" + createPdfUrl + "');");
            }
            else {
                nlapiLogExecution('DEBUG', 'Error', 'Internaal id of the record is null');
            }
    }
}


SUITELET SCRIPT:

function suitelet(request, response){
     var xml = "<?xml version=\"1.0\"?>\n<!DOCTYPE pdf PUBLIC \"-//big.faceless.org//report\" \"report-1.1.dtd\">\n";
     xml += "<pdf>";
     xml += "<head><macrolist><macro id=\"myfooter\"><p align=\"center\"><pagenumber /></p></macro></macrolist></head>";
     xml += "<body size= \"A4\" footer=\"myfooter\" footer-height=\"0.5in\">";
     var record = request.getParameter('internalId');
     xml +="record";       //Add values(in string format) what you want to show in pdf
     xml += "</body></pdf>";
     var file = nlapiXMLToPDF(xml);
     response.setContentType('PDF', 'Print.pdf ', 'inline');
     response.write(file.getValue()); 
}

提前致谢

1 个答案:

答案 0 :(得分:7)

我最近这样做的方式:

  • 用户事件添加调用suitelet的按钮(window.open('suitelet URL'))

  • Suitelet呈现自定义模板

你可以像这样做渲染一个Suitelet(params:request,response),custscript_pdf_template使用NetSuite高级HTML语法指向机柜上的html文件

    var template = nlapiGetContext().getSetting('SCRIPT', 'custscript_pdf_template');
    var purchaseOrder = nlapiLoadRecord('purchaseorder', tranId);
    var xmlTemplate = nlapiLoadFile(template);
    var renderer = nlapiCreateTemplateRenderer();
    var file;

    xmlTemplate = xmlTemplate.getValue();

    renderer.setTemplate(xmlTemplate);
    renderer.addRecord('record', purchaseOrder);

    xmlTemplate = renderer.renderToString();

    file = nlapiXMLToPDF(xmlTemplate);
    resObj = file.getValue();
    response.setContentType('PDF', 'printOut.pdf', 'inline');
    response.write(resObj)