In my react native application, I want to add a functionality to preview the PDF files. For that, I have an API url. When I open this url in the browser on desktop , it previews the Pdf file, but when I open this file in browser on my mobile, it starts downloading the file.
To preview the pdf, I am using ‘react-native-webview’.
const openLink = (cn) => {
const baseURL = 'http://192.168.2.81:7060/getCNToPreview';
const url = `${baseURL}/${cn?.fileName}`;
setDownloadPdf(true);
setPdfPath(url);
};
return (
<View>
<TouchableOpacity onPress=() => openLink(cn)> View </TouchableOpacity>
{
(downloadPdf && pdfPath !== '') && <WebView source={{ uri: pdfPath }} style= {styles.webView} />
}
</View>
)
In the openLink function, I am setting the url to preview the pdf. When the value of ‘downloadPdf’ is true and pdfPath is not “”, it should open the source path of pdf file in .
When it opens the file path in Webview, it starts downloading the file instead of previewing it. How can I achieve the functionality of previewing the pdf file in my react-native application.
Nitesh Gairola is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.