Skip to content

Commit

Permalink
fix ios direct url display
Browse files Browse the repository at this point in the history
  • Loading branch information
xcarpentier committed Jan 7, 2020
1 parent e271914 commit f97b877
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
3 changes: 2 additions & 1 deletion App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export default class App extends React.Component {
return (
<PdfReader
source={{
uri: 'http://gahp.net/wp-content/uploads/2017/09/sample.pdf',
// uri: 'http://gahp.net/wp-content/uploads/2017/09/sample.pdf',
base64,
}}
style={{ paddingTop: 20 }}
customStyle={{
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ interface Props {
source: Source
style?: View['props']['style'] // style props to override default container style
webviewStyle?: WebView['props']['style'] // style props to override default WebView style
webviewProps?: WebView['props']
noLoader?: boolean
customStyle?: {
readerContainer?: CSS.Properties
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rn-pdf-reader-js",
"version": "0.5.1",
"version": "0.5.2",
"description": "PDF reader for Expo",
"main": "node_modules/expo/AppEntry.js",
"types": "lib/index.d.ts",
Expand Down
46 changes: 34 additions & 12 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,41 +155,59 @@ interface Props {
interface State {
ready: boolean
data?: string
isBase64: boolean
}

class PdfReader extends React.Component<Props, State> {
state = {
ready: false,
data: undefined,
isBase64: false,
}

init = async () => {
try {
const { source, customStyle } = this.props
let ready = false
let data
let isBase64 = false
let data: any
if (
source.uri &&
(source.uri.startsWith('http') ||
source.uri.startsWith('file') ||
source.uri.startsWith('content'))
) {
data = await fetchPdfAsync(source)
ready = !!data
switch (Platform.OS) {
case 'android': {
data = await fetchPdfAsync(source)
ready = !!data
isBase64 = true
break
}

default: {
data = source.uri
ready = true
break
}
}
} else if (
source.base64 &&
source.base64.startsWith('data:application/pdf;base64,')
) {
data = source.base64
ready = true
isBase64 = true
} else {
alert('source props is not correct')
return
}

await writeWebViewReaderFileAsync(data!, customStyle)
if (isBase64) {
await writeWebViewReaderFileAsync(data!, customStyle)
}

this.setState({ ready, data })
this.setState({ ready, data, isBase64 })
} catch (error) {
alert(`Sorry, an error occurred. ${error.message}`)
console.error(error)
Expand All @@ -201,16 +219,19 @@ class PdfReader extends React.Component<Props, State> {
}

componentWillUnmount() {
try {
removeFilesAsync()
} catch (error) {
alert(`Error on removing file. ${error.message}`)
console.error(error)
const { isBase64 } = this.state
if (isBase64) {
try {
removeFilesAsync()
} catch (error) {
alert(`Error on removing file. ${error.message}`)
console.error(error)
}
}
}

render() {
const { ready, data } = this.state
const { ready, data, isBase64 } = this.state

const {
style: containerStyle,
Expand All @@ -220,11 +241,12 @@ class PdfReader extends React.Component<Props, State> {
onLoadEnd,
onError,
webviewProps,
source: { headers },
} = this.props

const originWhitelist = ['http://*', 'https://*', 'file://*', 'data:*']
const style = [styles.webview, webviewStyle]
const source = { uri: htmlPath }
const source = isBase64 ? { uri: htmlPath } : { uri: data!, headers }
const isAndroid = Platform.OS === 'android'
if (ready && data) {
return (
Expand Down

0 comments on commit f97b877

Please sign in to comment.