wonday / react-native-pdf

A <Pdf /> component for react-native
MIT License
1.58k stars 534 forks source link

Load from https uri Android #757

Closed Enragedsaturday closed 9 months ago

Enragedsaturday commented 11 months ago

What react-native version are you using? 72.1 What react-native-pdf version are you using? 6.7 patched What platform does your issue occur on? (android/ios/both) Android Describe your issue as precisely as possible : 1) Steps to reproduce the issue or to explain in which case you get the issue 2) Interesting logs Issue with react-native-blob-util not properly loading pdf from Https. Attempting to load from internet using an internet request to a file does not work. Manually using react-native-blob-util and the file streamed from a temp location works. Temporary workaround outlined below.

export const RenderPdf = ({route, navigation}) => {
  const pdfRef = useRef(null);
  const [pdfUrl, setPdfUrl] = useState(false);

  if (Platform.OS === 'android') {
    useEffect(() => {
      ReactNativeBlobUtil.config({
        // add this option that makes response data to be stored as a file,
        // this is much more performant.
        fileCache: true,
      })
        .fetch('GET', route.params.pdfUrl, {
          //some headers ..
        })
        .then(res => {
          let path = `file:${res.path()}`;
          setPdfUrl({uri: path, cache: false});
        });
    }, []);
  } else {
    useEffect(() => {
      setPdfUrl({uri: route.params.pdfUrl, cache: true});
    }, []);
  }

.....

{!pdfUrl && (
        <View style={{marginTop: 20}}>
          <ActivityIndicator size="large" />
        </View>
      )}
      {pdfUrl && (
        <Pdf
          source={pdfUrl}
          style={styles.pdf}
          ref={pdfRef}
          onPageChanged={onPageChanged}
          page={route.params.scrollPosition || 1}
        />
      )}
koreymadden commented 10 months ago

Have you tried using this prop for Pdf? trustAllCerts={false}

antonandreyev commented 9 months ago

trustAllCerts={false} worked for me

Enragedsaturday commented 9 months ago

Have you tried using this prop for Pdf? trustAllCerts={false}

Works, thanks

akinlekan28 commented 2 months ago

This should be included in the documentation.