Hello Suite people,
i am working on customer statement generation via dynamics were API triggers the script and returns base64 string to decode to pdf.
When i test via postman the base64 result string cannot decode, getting the following error
The MIME of file was detected as “text/plain”, but the decoder displays it as “application/pdf”.
keeping in mind that we have working method for sales orders and invoices and when i test in postman the result of base64 converts to pdf as it should.
any suggestion what would g wrong here?
/**
*@NApiVersion 2.x
*@NScriptType restlet
*/
define(['N/render', 'N/file', 'N/encode', 'N/record', 'N/log'], function (render, file, encode, record, log) {
var exports = {
get: getCustomerStatementPDF
};
function getCustomerStatementPDF(requestParams) {
log.audit({ title: "getCustomerStatementPDF" });
var entityId = requestParams.recordId; // Customer ID
var subsidiaryId = requestParams.subsidiaryId; // Subsidiary ID
var statementDate = requestParams.statementDate; // Statement date
var startDate = requestParams.startDate; // Start date
var customer = fetchCustomer(entityId);
if (!customer) {
return { error: "There is no customer with entity id " + entityId };
}
var customerPDFString = generateCustomerPDFString(customer, subsidiaryId, statementDate, startDate);
var base64EncodedCustomerPdfString = encode.convert({
string: customerPDFString,
inputEncoding: encode.Encoding.UTF_8,
outputEncoding: encode.Encoding.BASE_64
});
// Log the Base64 encoded string for debugging
log.debug({
title: "Base64 Encoded PDF String",
details: base64EncodedCustomerPdfString
});
return {
pdfBase64: base64EncodedCustomerPdfString // Return as a JSON object
};
}
function fetchCustomer(entityId) {
log.audit({ title: "fetchCustomer" });
try {
log.debug({
title: "fetchCustomer",
details: "fetchCustomer with EntityId: " + entityId
});
return record.load({
type: record.Type.CUSTOMER,
id: entityId // Use entityId to load the record
});
} catch (e) {
log.error({
title: "Error fetching Customer",
details: e.message
});
}
}
function generateCustomerPDFString(customer, subsidiaryId, statementDate, startDate) {
log.audit({ title: "generateCustomerPDFString" });
var customerId = customer.getValue({ fieldId: 'id' });
var customerRenderer = render.create();
var xmlTemplateFile = file.load({
id: '24180' // ID for the statement template
});
customerRenderer.templateContent = xmlTemplateFile.getContents();
// Add the customer record and additional parameters to the renderer
customerRenderer.addRecord({
templateName: 'record',
record: customer // This remains unchanged
});
var pdfFile = customerRenderer.renderAsPdf();
return pdfFile.getContents();
}
return exports;
});