I'm trying to download a file in PDF from LexOffice API via Lambda Function (NodeJS, and Amplify) and return the result to the user who called the Lambda.
I am using NextJS with Typescript.
So this is the Lambda snippet:
const resInvoice = await axios.get(`https://api.lexoffice.io/v1/files/${invoiceFileId}`, {"headers": {'Authorization': `Bearer ${lexOfficeApiKey}`,'Accept': 'application/pdf', }, }); console.log(resInvoice) return { statusCode: 200, headers: {"Access-Control-Allow-Headers": "*","Access-Control-Allow-Origin": "*","Access-Control-Allow-Methods": "OPTIONS,POST,GET","Content-Type": "application/pdf", }, body: resInvoice.data.toString('base64'), isBase64Encoded: true };
And here how I handle calling the Lambda and download the PDF.
const restOperation = post({ apiName: 'TeachpieRestApi', path: '/getinvoicepdf', options: { body: { queryParameters: {"invoiceId": invoiceId,"invoideFileId": invoideFileId, }, } }, }); const { body } = await restOperation.response; const blobPDF = await body.blob() // set the blog type to final pdf const file = new Blob([blobPDF], { type: 'application/pdf' }); // process to auto download it const fileURL = URL.createObjectURL(blobPDF); let link = document.createElement('a'); link.href = fileURL; link.download = "Test" +".pdf"; link.click();