Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
DhruvKalpeshThakar opened this issue Jan 17, 2025 · 0 comments
Open

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

DhruvKalpeshThakar opened this issue Jan 17, 2025 · 0 comments

Comments

@DhruvKalpeshThakar
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant