I am using react-native-html-to-pdf
to generate a PDF from an HTML string. It works file and I can see that the generated PDF path is /storage/emulated/0/Android/data/**MY_PACKAGE_NAME**/files/TestFolder/mytest_print.pdf
as shown in the code below:
import RNHTMLtoPDF from 'react-native-html-to-pdf';
import ReactNativeBlobUtil from 'react-native-blob-util'
//... OTHER CODE
let options = {
html: `<h1>Test PDF HTML</h1``,
fileName: `mytest_print `,
directory: 'TestFolder',
};
RNHTMLtoPDF.convert(options)
.then((file) => {
const fileToPrint = {
filename: options.fileName, // file name
path: file.filePath, // .pdf file path
type: 'file', // `file` or `directory`
}
ReactNativeBlobUtil.fs.readFile(fileToPrint).then((pdfData) => {
/// It never enters here
ReactNativeBlobUtil.fs.readFile(file.filePath, 'base64').then((pdfData) => {
const shareOptions = {
title: 'My PDF File',
url: `data:application/pdf;base64,${pdfData}`,
type: 'application/pdf',
};
Share.share(shareOptions)
.then((result) => {
console.log('Share result:', result);
})
.catch((error) => {
alert('Share error:', error);
});
}).catch((error) => {
alert("Error: ", error);
})
}).catch((error) => {
alert("Error: ", error);
})
})
I get Possible Unhandled Promise Rejection (id: 2): TypeError: Missing argument "path"
or Possible Unhandled Promise Rejection (id: 0): TypeError: undefined is not a function
as errors. So not sure why pdfData
is not being fetched correctly even though I can see filePath
exist. I also have permissions in my AndroidManifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
...
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
but still not able to read the PDF file.