react-native-nitro-pdf
v0.1.1
Published
react-native-nitro-pdf is a react native package built with Nitro
Maintainers
Readme
react-native-nitro-pdf
react-native-nitro-pdf is a react native package built with Nitro
Requirements
- React Native v0.76.0 or higher
- Node 18.0.0 or higher
[!IMPORTANT]
To SupportNitro Viewsyou need to install React Native version v0.78.0 or higher.
Installation
bun add react-native-nitro-pdf react-native-nitro-modulesUsage
Import
import { NitroPdf, PdfDocument } from 'react-native-nitro-pdf'NitroPdf (view)
Render a PDF by passing a local file path or an http(s) URL.
import React from 'react'
import { NitroPdf } from 'react-native-nitro-pdf'
import { View, StyleSheet } from 'react-native'
export function Example() {
return (
<View style={styles.container}>
<NitroPdf
source="https://example.com/document.pdf"
fitPolicy="both"
backgroundColor="#F5F5F5"
onLoadSuccess={(metadata) => {
console.log('pageCount', metadata.pageCount)
console.log('aspectRatio', metadata.aspectRatio)
}}
onLoadError={(message) => {
console.warn('Failed to load PDF:', message)
}}
/>
</View>
)
}
const styles = StyleSheet.create({
container: {
height: 400,
width: 300,
},
})Props:
source: string- Local file path orhttp(s)URL to the PDF.fitPolicy: 'width' | 'height' | 'both'- How the PDF is fit inside the view (letterboxed using the computed page aspect ratio).backgroundColor?: string- Background for the letterbox bars (examples:'#FFFFFF','#AARRGGBB').onLoadSuccess?: (metadata: PdfMetadata) => void- Called after the PDF is opened and metadata is extracted.onLoadError?: (message: string) => void- Called when downloading/opening/parsing fails.
PdfMetadata includes:
pageCount: numberpageSize: { width: number; height: number }aspectRatio: numberfilePath: string- Resolved local path (cached/downloaded when needed)
PdfDocument (object)
Use PdfDocument to pre-load metadata and manage the on-device cache.
import { PdfDocument } from 'react-native-nitro-pdf'
const doc = new PdfDocument()
async function preload() {
const metadata = await doc.load('https://example.com/document.pdf')
console.log(metadata.pageCount)
}
function clear() {
doc.clearCache()
}Credits
Bootstrapped with create-nitro-module.
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
