Search code examples
javascriptreactjspdfpowerbipowerbi-embedded

Export to PDF by using Rest API(send to file) in my react project is getting blank PDF


API returned PDF data sample looks like -

Api response


Below is JavaScript code which I am using to download PDF, I tried many solutions, but none of them worked.

const reportPdfDoc = await axiosBIInstance.get(
            `https://api.powerbi.com/v1.0/myorg/reports/${config.reportId}/exports/${exportID?.data?.id}/file`,
            {
              headers: {
                Authorization: 'Bearer ' + accessToken,
                responseType: 'arraybuffer',// I've used blob as well, but same result
              },
            },
          );
          if (reportPdfDoc?.status === 200) {
            const blob = new Blob([reportPdfDoc.data], {
              type: 'application/pdf',
            });
            
         
            const computedFileName = `${exportStatus?.data?.reportName}.pdf`;
            const a = document.createElement('a');
            a.href = window.URL.createObjectURL(blob);
            a.download = computedFileName;
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);

Solution

  • I tried the solution posted above and it worked for me.I am reposting it below.

    I got the issue, responseType property should be outside of header in axios call. like -

    const reportPdfDoc = await axiosBIInstance.get( https://api.powerbi.com/v1.0/myorg/reports/${config.reportId}/exports/${exportID?.data?.id}/file, { headers: { Authorization: 'Bearer ' + accessToken, }, responseType: 'blob', }, );