How convert base64 to pdf React-Native

Viewed 9673

I created an app using react-native and have some base64 data from api. I want convert base64 data to pdf format. if you have any idea please help me. thanks.

2 Answers

You can use react-native-pdf package (https://www.npmjs.com/package/react-native-pdf). If you want to show the pdf in your app , this package would be quite helpful as it supports loading PDFs from base64 string for both ios and android . You can specify the pdf's source from your base64 data as shown in their example :

{uri:"data:application/pdf;base64,JVBERi0xLjcKJc..."}

I'm assuming, converting to PDF as in saving the base64 format data as PDF in some file.

the following code can help you do the same,

let fPath = Platform.select({
   ios: fs.dirs.DocumentDir,
   android: fs.dirs.DownloadDir,
});

fPath = `${fPath}/pdfFileName.pdf`;

if (Platform.OS === PlatformTypes.IOS) {
    await fs.createFile(fPath, base64Data, "base64");
} else {
    await fs.writeFile(fPath, base64Data, "base64");
}
// RNFetchBlob can be used to open the file
Related