I am encountering a difficulty with downloading and naming PDF recordsdata in a React Native undertaking. I am utilizing the ReactNativeBlobUtil
library for dealing with file operations. The performance works easily on Android, however I am dealing with challenges on iOS.
Here is a snippet of the related code:
import ReactNativeBlobUtil from 'react-native-blob-util';
const generatePdfConfig = (receipt: Receipt) => {
const path = generatePdfPath(receipt.receipt_number);
const config = ReactNativeBlobUtil.config({
fileCache: true,
appendExt: 'pdf',
addAndroidDownloads: {
title: `${receipt.receipt_number}.pdf`,
description: 'Downloading file...',
mime: 'utility/pdf',
useDownloadManager: true,
mediaScannable: true,
notification: true,
path,
},
});
return config;
};
/**
* Handles the obtain and preview of a PDF receipt.
* @param receipt - The receipt object.
*/
export const handleDownloadPdf = async (receipt: Receipt) => {
strive {
// Generate the PDF configuration
const config = generatePdfConfig(receipt);
// Fetch the receipt URL to obtain the PDF
const res = await config.fetch('GET', receipt.receipt_url);
const path = res.path();
// Preview the PDF primarily based on the platform
if (Platform.OS === 'ios') {
// Preview the PDF on iOS
ReactNativeBlobUtil.ios.previewDocument(path);
} else {
// Open the PDF utilizing Android's default PDF viewer
ReactNativeBlobUtil.android.actionViewIntent(path, 'utility/pdf');
}
} catch (error) {
showToast({
sort: 'error',
textual content: 'Can not obtain receipt, please strive once more!',
});
}
};
enter picture description right here IOS screenshot
What I Tried:
I tried to obtain and title PDF recordsdata in a React Native undertaking utilizing the ReactNativeBlobUtil
library. For Android, I utilized the addAndroidDownloads
configuration with the desired title (title:
${receipt.receipt_number}.pdf“). On iOS, I relied on the default conduct of the ReactNativeBlobUtil
library.
Anticipated Final result:
I anticipated the PDF recordsdata to be downloaded and named in keeping with the offered configuration on each Android and iOS. Particularly, I anticipated the iOS file to be named utilizing the receipt quantity.
Precise Final result:
Whereas the Android performance labored as anticipated, the iOS file was named with a seemingly random alphanumeric string. Regardless of makes an attempt to change the file title on iOS, I encountered challenges, and the specified naming conference was not achieved.