Is it possible to generate and return an invoice P...
# suitescript
d
Is it possible to generate and return an invoice PDF via a restlet if just returning it as a base64 string?
d
it should be possible
u
yup. PDF's a just XML strings so generate/render as PDF string and base64 code the PDF string and have it as you response on the RESTlet
b
careful, while BFO uses xml to generate a pdf, pdfs are not xml
there is a binary component
Use one of the N/render methods that outputs a pdf file, get its contents, and set a key on your json output to that content
u
Yup. Limit images unless you use the api renderAsString() and
b
it will already be base64 encoded
d
cool - thanks. It's just that when I "new up" an instance of renderer it turns out to be null
const renderer = render.create();
so I want to be sure it's not a limitation in a restlet itself.
b
huh
its an object
where are you getting null from
d
I can see the
null
in the execution log. My restlet ended up littering it with log.debug's after my restlet gets this exception returned in Postman:
Copy code
{
  "error": {
    "code": "UNEXPECTED_ERROR",
    "message": "{\"type\":\"error.SuiteScriptError\",\"name\":\"UNEXPECTED_ERROR\",\"message\":null,\"stack\":[\"renderXmlToPdf(N/render)\",\"<anonymous>(/SuiteScripts/services/invoice-service.js:145)\",\"<anonymous>(/SuiteScripts/restlets/invoice-pdf.js:21)\"],\"cause\":{\"type\":\"internal error\",\"code\":\"UNEXPECTED_ERROR\",\"details\":null,\"userEvent\":null,\"stackTrace\":[\"renderXmlToPdf(N/render)\",\"<anonymous>(/SuiteScripts/services/invoice-service.js:145)\",\"<anonymous>(/SuiteScripts/restlets/invoice-pdf.js:21)\"],\"notifyOff\":false},\"id\":\"401cd5d6-fff6-4d63-bb5e-4d71102aed6e-2d323032302e30342e3137\",\"notifyOff\":false,\"userFacing\":false}"
  }
}
code looks like this:
Copy code
public getInvoicePdf(invoicePdfDto: InvoicePdfDto): InvoicePdfCreatedDto {
        
        log.debug('getInvoicePdf', { render: render});
        const renderer = render.create();

        log.debug('renderer', { renderer: renderer});
        renderer.addRecord( { templateName: 'record', record: record.load({
                type: record.Type.INVOICE,
                id: 483830 // TODO: map invoicePdfDto.invoiceExternalId to internalId
            })
        });
        renderer.setTemplateById( { id: 21 });
       
        const invoicePdf = renderer.renderAsPdf();

        log.debug('invoicePdf', { invoicePdf: invoicePdf});

        // write file        
        return <InvoicePdfCreatedDto> {
            invoiceId: 483830,
            invoiceExternalId: invoicePdfDto.invoiceExternalId,
            invoicePdfBase64: invoicePdf.getContents()
        }
    }
it dies in the line after
log.debug('renderer', { renderer: renderer});
b
you would get an error about renderer being null, not an internal unexpected error
many netsuite objects implement a .toJSON method, so what you are logging is the output of that function, not the actual object itself
d
I edited the text above - internal unexpected is the postman response, the
null
is what I'm seeing in the execution logs
many netsuite objects implement a .toJSON method, so what you are logging is the output of that function, not the actual object itself
ok so this could be a redherring
b
your code looks reasonable, so i recommend starting off with the standard invoice template (2)
d
ok, so the execution log was a massive redherring. thanks @battk - you were spot on with the invoice template id, now get my base64 code returned
172 Views