-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add html rendering support (#203)
Signed-off-by: Sai Ranjit Tummalapalli <[email protected]>
- Loading branch information
Showing
10 changed files
with
208 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import type { StackScreenProps } from '@react-navigation/stack' | ||
|
||
import React, { useEffect, useState } from 'react' | ||
import { ActivityIndicator, Platform, StyleSheet, TouchableOpacity, View } from 'react-native' | ||
import RNHTMLtoPDF from 'react-native-html-to-pdf' | ||
import { SafeAreaView } from 'react-native-safe-area-context' | ||
import Share, { ShareOptions } from 'react-native-share' | ||
import Icon from 'react-native-vector-icons/MaterialIcons' | ||
import WebView from 'react-native-webview' | ||
|
||
import { ColorPallet } from '../theme' | ||
import { CredentialStackParams, Screens } from '../types/navigators' | ||
|
||
type RenderCertificateProps = StackScreenProps<CredentialStackParams, Screens.RenderCertificate> | ||
|
||
const defaultHtmlContent = ` | ||
<div style="width:800px; height:600px; padding:20px; text-align:center; border: 10px solid #787878"> | ||
</div> | ||
` | ||
|
||
const RenderCertificate: React.FC<RenderCertificateProps> = ({ navigation, route }) => { | ||
if (!route?.params) { | ||
throw new Error('RenderCertificate route prams were not set properly') | ||
} | ||
|
||
const { credential } = route?.params | ||
const [htmlContent, setHtmlContent] = useState(defaultHtmlContent) | ||
const [loader, setLoader] = useState(false) | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
}, | ||
}) | ||
|
||
const fetchHtmlContent = async () => { | ||
try { | ||
const certificateAttributes = credential.credential.credentialSubject.claims | ||
|
||
let content = credential.credential.prettyVc | ||
|
||
Object.keys(certificateAttributes).forEach(key => { | ||
// Statically picking the value of placeholder | ||
const placeholder = `{{credential['${key}']}}` | ||
// Escaping the placeholder to avoid regex issues | ||
const escapedPlaceholder = placeholder.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') | ||
// Replacing the placeholder with the actual value | ||
content = content.replace(new RegExp(escapedPlaceholder, 'g'), certificateAttributes[key]) | ||
}) | ||
|
||
setHtmlContent(content) | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error('Error mapping HTML content:', error) | ||
} | ||
} | ||
|
||
const downloadHtmlToPdf = async () => { | ||
try { | ||
setLoader(true) | ||
const options: RNHTMLtoPDF.Options = { | ||
html: htmlContent, | ||
fileName: credential.credential.type[1], | ||
directory: 'Documents', | ||
} | ||
|
||
const file = await RNHTMLtoPDF.convert(options) | ||
|
||
let filePath = file.filePath as string | ||
|
||
if (Platform.OS === 'android') { | ||
filePath = 'file://' + filePath | ||
} | ||
|
||
setLoader(false) | ||
|
||
const shareOptions: ShareOptions = { url: filePath } | ||
await Share.open(shareOptions) | ||
} catch (e) { | ||
setLoader(false) | ||
// eslint-disable-next-line no-console | ||
console.log('error downloading html to pdf', e) | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
navigation.setOptions({ | ||
headerRight: () => | ||
loader ? ( | ||
<View style={{ marginRight: 20 }}> | ||
<ActivityIndicator size="small" color={ColorPallet.grayscale.white} /> | ||
</View> | ||
) : ( | ||
<TouchableOpacity onPress={downloadHtmlToPdf} style={{ marginRight: 20 }}> | ||
<Icon style={{ color: ColorPallet.grayscale.white }} size={25} name="download" /> | ||
</TouchableOpacity> | ||
), | ||
}) | ||
}, [navigation, htmlContent, loader]) | ||
|
||
useEffect(() => { | ||
fetchHtmlContent() | ||
}, []) | ||
|
||
return ( | ||
<SafeAreaView style={styles.container} edges={['left', 'right']}> | ||
<WebView source={{ html: htmlContent }} originWhitelist={['*']} /> | ||
</SafeAreaView> | ||
) | ||
} | ||
|
||
export default RenderCertificate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3574,6 +3574,11 @@ | |
resolved "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.5.tgz" | ||
integrity sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w== | ||
|
||
"@types/react-native-html-to-pdf@^0.8.3": | ||
version "0.8.3" | ||
resolved "https://registry.yarnpkg.com/@types/react-native-html-to-pdf/-/react-native-html-to-pdf-0.8.3.tgz#0e41b666c711b114957e42f7a7c665fb2fea8045" | ||
integrity sha512-KjyR1F9KhcmX8p9y/8WYMiaK4DV/cYBUO1XHEzD9dDVZBkY9ujbdMLYO7ZvmAffNT2Q484Qvg+t6szgVcnN22g== | ||
|
||
"@types/react-native-vector-icons@^6.4.13": | ||
version "6.4.13" | ||
resolved "https://registry.npmjs.org/@types/react-native-vector-icons/-/react-native-vector-icons-6.4.13.tgz" | ||
|
@@ -9866,6 +9871,11 @@ react-native-gifted-chat@^2.4.0: | |
use-memo-one "1.1.3" | ||
uuid "3.4.0" | ||
|
||
react-native-html-to-pdf@^0.12.0: | ||
version "0.12.0" | ||
resolved "https://registry.yarnpkg.com/react-native-html-to-pdf/-/react-native-html-to-pdf-0.12.0.tgz#2b467296f85c9c9783a7288b19722a7028dcbcb8" | ||
integrity sha512-Yb5WO9SfF86s5Yv9PqXQ7fZDr9zZOJ+6jtweT9zFLraPNHWX7pSxe2dSkeg3cGiNrib65ZXGN6ksHymfYLFSSg== | ||
|
||
[email protected]: | ||
version "1.3.1" | ||
resolved "https://registry.npmjs.org/react-native-iphone-x-helper/-/react-native-iphone-x-helper-1.3.1.tgz" | ||
|