Search code examples
react-nativepdfreact-native-pdf

react-native-pdf file not in PDF format or corrupted


I can open the link: https://www.orimi.com/pdf-test.pdf, but when I try with the link: http://94.23.154.57:8081/digital-cert/download-file?id=33FB3234-23EC-472B-AB6F-76436DDCFA45&view=1, I cannot open it. I receive an error message: Error: cannot create a document: File not in PDF format or corrupted. I suspect that HTTP might be causing the issue, but the PDF link is returned from the server and I cannot change it to HTTPS

import Pdf from 'react-native-pdf';
import {
  View,
} from 'react-native';

const PDFExample = ({route}) => {
  const source = {
    uri: 'http://94.23.154.57:8081/digital-cert/download-file?id=33FB3234-23EC-472B-AB6F-76436DDCFA45&view=1',
    cache: true,
  };
  return (
      <View style={styles.container}>
        <Pdf
          trustAllCerts={false}
          source={source}
          onLoadComplete={(numberOfPages, filePath) => {
            console.log(`Number of pages: ${numberOfPages}`);
          }}
          onPageChanged={(page, numberOfPages) => {
            console.log(`Current page: ${page}`);
          }}
          onError={error => {
            console.log(error);
          }}
          onPressLink={uri => {
            console.log(`Link pressed: ${uri}`);
          }}
          style={styles.pdf}
        />
      </View>
)
}

Solution

  • PDF plugin won't open corrupted format PDF, Even I tried downloading and opening file:// but it did not work for me.

    This may also indicate that the backing PDF file is missing or partially downloaded, there are many reasons for PDF file corruption. I found 6 reasons

    1. Errors during download
    2. Using incompatible software
    3. Encoding by some email service providers
    4. Hard disk problem
    5. Attacks by viruses
    6. Opening the same file with different software

    If you want to repair PDF then check this docs by Adobe Repair a Damaged PDF.

    And if you do not want to do all these things then you can open the PDF in another way also.

    We have three options to open PDF in React Native, the first one you have already done, now you can choose the other two

    • react-native-webview

    • react-native-file-viewer

    If you choose webview then check this answer by aytek answer

    and you can open the pdf using file viewer

    Example:

     const path = FileViewer.open(path, { showOpenWithDialog: true }) // absolute-path-to-my-local-file.
      .then(() => {
        // success
      })
      .catch((error) => {
        // error
      });