Skip to content

On iOS PDF File getting opened but immediatley closes. #165

@DhruvKalpeshThakar

Description

@DhruvKalpeshThakar

export const downloadSalarySlip = (request) => async (dispatch) => {
try {
const userinfo = await AsyncStorage.getItem('LoginData');
const token = JSON.parse(userinfo)?.token;

const response = await makeAPIRequest({
  method: GET,
  url: `${api.downloadSalarySlipApi}?user_id=228`,
  headers: {
    'Content-Type': 'application/pdf',
    Authorization: `Bearer ${token}`,
  },
  responseType: 'arraybuffer',
});

if (response?.data) {
  const filePath = `${RNFS.DocumentDirectoryPath}/SalarySlip_${Date.now()}.pdf`;
  console.log('File path:', filePath);

  const buffer = Buffer.from(response.data);
  await RNFS.writeFile(filePath, buffer.toString('base64'), 'base64');
  console.log('File saved successfully at:', filePath);

  // Introduce a delay of 300ms before opening the file
  setTimeout(() => {
    FileViewer.open(filePath, { type: 'application/pdf' })
      .then(() => {
        console.log('PDF opened successfully!');
        if (request.onSuccess) request.onSuccess(filePath);
      })
      .catch((error) => {
        console.error('Error opening PDF:', error);
        if (request.onFail) request.onFail('Error opening PDF');
      });
  }, 300); // 300ms delay
} else {
  console.error('No data in API response');
  if (request.onFail) request.onFail('No data in API response');
}

} catch (error) {
console.error('Error downloading salary slip:', error);
if (request.onFail) request.onFail(error.message);
}
};

I have tried adding delay of 300ms to manage but its still not working

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions