0

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.

Chaudhry Talha
  • 7,231
  • 11
  • 67
  • 116
  • This makes no sense. If an app can create/write a file in app specific storage then it can also read it. And for app specific storage you do not need any permission. – blackapps Jun 04 '23 at 13:23
  • @blackapps That's exactly what I am wondering as I cannot tell why this error is very generic. If I do `RNPrint.print({ filePath: file.filePath })` from `react-native-print` it also works find. Am I setting the file type correctly? My file path is of `.pdf` file. – Chaudhry Talha Jun 04 '23 at 14:26
  • You think we know the value of file.filePath? – blackapps Jun 04 '23 at 15:15

0 Answers0