4

My Code:

function combineExpenseFiles(type){
    try{
        var currentRecordId = nlapiGetRecordId();
        var currentRecord = nlapiLoadRecord('expensereport',currentRecordId);
        var expensesLineItemCount = currentRecord.getLineItemCount('expense');
        var template = '';
        var fileParts = new Array();
        template += '<?xml version="1.0"?><!DOCTYPE pdf PUBLIC "-//big.faceless.org//report" "report-1.1.dtd">\n';
        template += '<pdf>\n<head>\n';
        template += '</head>\n<body>\n';
        flag=1;
        for(var i=1;i<=expensesLineItemCount;i++){
            var fileId = currentRecord.getLineItemValue('expense','expmediaitem',i);
            var file = nlapiLoadFile(fileId); 
            var name = file.getName();

            //var fileUrl = file.getURL();

            nlapiLogExecution('DEBUG','online',file.isOnline());
            var fileUrl = '/core/media/media.nl?id=36333&c=3960047&h=487697f81e113499d5f4';
            template += '<img src="';
            template += nlapiEscapeXML(fileUrl);
            template += '" width="10%" height="10%"></img>\n<br></br><br></br>';
            break;
        }
        template += '</body>\n</pdf>';
        nlapiLogExecution('DEBUG','filename',template);

        var renderer = nlapiCreateTemplateRenderer(); 
        renderer.setTemplate(template); 
        //renderer.addRecord('url',url);
        var xml = renderer.renderToString();
        nlapiLogExecution('DEBUG','xml',xml);
        var combinefile = nlapiXMLToPDF(xml);



        combinefile.setEncoding('UTF-8');
        combinefile.setName('expense_f.pdf');
        nlapiLogExecution('DEBUG','file',combinefile);
        combinefile.setFolder(2221); // Amit_Expenses

        nlapiSubmitFile(combinefile);

    }catch(error){
        //nlapiLogExecution('DEBUG','error1',error.getDetails());
        nlapiLogExecution('DEBUG','error',error.toString());
    }    
}

What this code is doing that it takes all 'jpeg' files related to an expense and create template containing all the files and generate a .pdf file containing all the images. Code is working perfectly fine.

For now I have put the file url hardcoded for testing purpose.

The problem is that code is not able to refer images for certain folders. Like if I am refer employees profile images it refers them but for some folders code is not able to refer images of those folders.

I have checked that these folders are not private. Please Suggest

Deepak Garg
  • 142
  • 8

1 Answers1

1

From my experience, I had to set "Available Without Login" on any image I need to be rendered in the PDF. I can't explain the deeper reason as to why. I can only tell you how I've been able to work around the issue.

I believe it has something to do with the way NetSuite implemented the XML to PDF generator. It seems it does not run in the same context of the main script. But like I stated above, I'm not sure why.

Hopefully you find this helpful.

scheppsr77
  • 577
  • 2
  • 12