### Install Dependencies for iOS Example Source: https://github.com/wonday/react-native-pdf/blob/master/README.md Run these commands in the project folder to ensure all dependencies are available before running the iOS example. ```bash yarn install (or npm install) cd ios pod install cd .. react-native run-ios ``` -------------------------------- ### Install react-native-pdf and react-native-blob-util Source: https://github.com/wonday/react-native-pdf/blob/master/README.md Use npm or yarn to install the necessary packages. Ensure both react-native-pdf and react-native-blob-util are installed. ```bash # Using npm npm install react-native-pdf react-native-blob-util --save # or using yarn: yarn add react-native-pdf react-native-blob-util ``` -------------------------------- ### Example: Network Request with Authentication Headers Source: https://github.com/wonday/react-native-pdf/blob/master/_autodocs/configuration.md Configure a network request for a PDF with custom authentication and user-agent headers. ```javascript { uri: 'https://api.example.com/pdf', cache: true, method: 'GET', headers: { 'Authorization': 'Bearer eyJhbGc...', 'User-Agent': 'MyApp/1.0', 'X-Custom-Header': 'value' } } ``` -------------------------------- ### iOS Installation for React Native 0.60+ Source: https://github.com/wonday/react-native-pdf/blob/master/README.md For React Native 0.60 and above, simply run 'pod install' in the ios directory. Linking is handled automatically. ```bash pod install ``` -------------------------------- ### Scalable Content Example Source: https://github.com/wonday/react-native-pdf/blob/master/_autodocs/api-reference/PinchZoomView.md Demonstrates how to use PinchZoomView to make content scalable with pinch gestures. It tracks and displays the current scale. ```javascript import React from 'react'; import { View, Text } from 'react-native'; import PinchZoomView from 'react-native-pdf/PinchZoomView'; export function ScalableContent() { const [scale, setScale] = React.useState(1); return ( { setScale(scale => scale * pinchScale); }} style={{ flex: 1 }} > Pinch me! Scale: {scale.toFixed(2)}x ); } ``` -------------------------------- ### Basic PDF Viewer Configuration Source: https://github.com/wonday/react-native-pdf/blob/master/_autodocs/README.md A simple example of how to render a PDF from a URI using the Pdf component with basic styling. ```javascript ``` -------------------------------- ### Example: 24-hour Cache Configuration Source: https://github.com/wonday/react-native-pdf/blob/master/_autodocs/configuration.md Configure caching for a PDF with a custom filename and a 24-hour expiration time. ```javascript { uri: 'https://example.com/document.pdf', cache: true, cacheFileName: 'my-document-v1', expiration: 86400 // 24 hours in seconds } ``` -------------------------------- ### Start Metro Bundler Source: https://github.com/wonday/react-native-pdf/blob/master/FabricExample/README.md Run this command from the root of your React Native project to start the Metro dev server. This is essential for the development workflow. ```sh # Using npm npm start # OR using Yarn yarn start ``` -------------------------------- ### Install react-native-pdf with yarn Source: https://github.com/wonday/react-native-pdf/blob/master/_autodocs/INDEX.md Install the react-native-pdf library and its peer dependency react-native-blob-util using yarn. Ensure you have yarn installed. ```bash yarn add react-native-pdf react-native-blob-util ``` -------------------------------- ### Set Initial Page Source: https://github.com/wonday/react-native-pdf/blob/master/_autodocs/configuration.md Specify the starting page for the PDF viewer using the `page` prop. ```javascript // Start at page 5 ``` -------------------------------- ### Install CocoaPods Dependencies for iOS Source: https://github.com/wonday/react-native-pdf/blob/master/FabricExample/README.md Before building the iOS app, install the necessary CocoaPods dependencies. Run 'bundle install' once to install the bundler, and 'bundle exec pod install' subsequently or after updating native dependencies. ```sh bundle install ``` ```sh bundle exec pod install ``` -------------------------------- ### UWP Bundled Asset Example Source: https://github.com/wonday/react-native-pdf/blob/master/_autodocs/api-reference/Pdf.md For Windows (UWP) applications, use the `ms-appx:///` scheme to reference PDF files bundled with the app. ```javascript { uri: "ms-appx:///assets/file.pdf" } ``` -------------------------------- ### Install react-native-pdf with npm Source: https://github.com/wonday/react-native-pdf/blob/master/_autodocs/INDEX.md Install the react-native-pdf library and its peer dependency react-native-blob-util using npm. Ensure you have npm installed. ```bash npm install react-native-pdf react-native-blob-util ``` -------------------------------- ### onScaleChanged Callback Example Source: https://github.com/wonday/react-native-pdf/blob/master/_autodocs/api-reference/PinchZoomView.md Example of how to implement the onScaleChanged callback to log pinch gesture details like scale, pageX, and pageY to the console. ```javascript onScaleChanged={({scale, pageX, pageY}) => { console.log(`Pinch scale: ${scale.toFixed(2)}x at (${pageX}, ${pageY})`); }} ``` -------------------------------- ### Static Require() Example Source: https://github.com/wonday/react-native-pdf/blob/master/_autodocs/api-reference/Pdf.md Use the `require()` function to reference a local PDF file. This method is primarily supported on iOS. ```javascript source={require('./assets/file.pdf')} ``` -------------------------------- ### Bundled App Assets Example Source: https://github.com/wonday/react-native-pdf/blob/master/_autodocs/api-reference/Pdf.md Reference a PDF file that is bundled with your application's assets. This is supported on iOS and Android. ```javascript { uri: "bundle-assets://pdfs/document.pdf" } ``` -------------------------------- ### Example Usage of scrollToXY Source: https://github.com/wonday/react-native-pdf/blob/master/_autodocs/api-reference/PdfViewFlatList.md Demonstrates how to use the scrollToXY method with a ref to the PdfViewFlatList component to scroll to a specific position. ```javascript const flatListRef = useRef(null); // Scroll to position (200, 300) flatListRef.current.scrollToXY(200, 300); ``` -------------------------------- ### Full-Featured PDF Viewer Configuration Source: https://github.com/wonday/react-native-pdf/blob/master/_autodocs/README.md An example showcasing advanced configuration options for the Pdf component, including zoom, paging, and various event handlers. ```javascript console.log(`${p*100}%`)} onLoadComplete={(pages, path) => console.log(`${pages} pages`)} onPageChanged={(page, total) => console.log(`Page ${page}/${total}`)} onError={(err) => console.error(err)} onPageSingleTap={(page, x, y) => console.log(`Tapped page ${page}`)} onScaleChanged={(scale) => console.log(`Zoom: ${scale}x`)} onPressLink={(url) => Linking.openURL(url)} /> ``` -------------------------------- ### Basic PdfPageView Usage Source: https://github.com/wonday/react-native-pdf/blob/master/_autodocs/api-reference/PdfPageView.md Example demonstrating how to use the PdfPageView component to render a single PDF page. Ensure you have the fileNo from PdfManager.loadFile(). ```javascript import React from 'react'; import PdfPageView from 'react-native-pdf/PdfPageView'; export function SinglePageViewer({fileNo}) { return ( ); } ``` -------------------------------- ### Absolute File System Path Example Source: https://github.com/wonday/react-native-pdf/blob/master/_autodocs/api-reference/Pdf.md Specify an absolute path to a PDF file on the device's file system. Ensure the path is correctly formatted with the `file:///` prefix. ```javascript { uri: "file:///sdcard/Documents/file.pdf" } ``` -------------------------------- ### High-Zoom UI Configuration Source: https://github.com/wonday/react-native-pdf/blob/master/_autodocs/configuration.md This setup allows for higher zoom levels and enables double-tap to zoom, suitable for detailed document viewing. ```javascript ``` -------------------------------- ### Remote PDF URI Example Source: https://github.com/wonday/react-native-pdf/blob/master/_autodocs/api-reference/Pdf.md Use this format to specify a PDF located at a remote URL via HTTP or HTTPS. ```javascript { uri: "https://example.com/file.pdf" } ``` -------------------------------- ### Windows Installation for RNW 0.62 Source: https://github.com/wonday/react-native-pdf/blob/master/README.md Specific instructions for integrating react-native-pdf and react-native-blob-util into a Windows project using Visual Studio 2019, particularly for RNW 0.62. ```csharp - Open your solution in Visual Studio 2019 (eg. `windows\yourapp.sln`) - Right-click Solution icon in Solution Explorer > Add > Existing Project... - If running RNW 0.62: add `node_modules\react-native-pdf\windows\RCTPdf\RCTPdf.vcxproj` - If running RNW 0.62: add `node_modules\react-native-blob-util\windows\ReactNativeBlobUtil\ReactNativeBlobUtil.vcxproj` - Right-click main application project > Add > Reference... - Select `progress-view` and in Solution Projects - If running 0.62, also select `RCTPdf` and `ReactNativeBlobUtil` - In app `pch.h` add `#include "winrt/RCTPdf.h" - If running 0.62, also select `#include "winrt/ReactNativeBlobUtil.h"` - In `App.cpp` add `PackageProviders().Append(winrt::progress_view::ReactPackageProvider());` before `InitializeComponent();` - If running RNW 0.62, also add `PackageProviders().Append(winrt::RCTPdf::ReactPackageProvider());` and `PackageProviders().Append(winrt::ReactNativeBlobUtil::ReactPackageProvider());` ``` -------------------------------- ### Base64 Encoded PDF Example Source: https://github.com/wonday/react-native-pdf/blob/master/_autodocs/api-reference/Pdf.md Provide a PDF directly as a Base64 encoded string. The `data:` URI scheme must be used with the correct MIME type. ```javascript { uri: "data:application/pdf;base64,JVBERi0xLjc..." } ``` -------------------------------- ### Usage Example for iOS PdfViewer Source: https://github.com/wonday/react-native-pdf/blob/master/_autodocs/api-reference/PdfView.md Demonstrates how to use the PdfView component to display a PDF file on iOS. It includes handling load completion, page changes, and errors, along with setting display properties like fitPolicy and spacing. ```javascript import React from 'react'; import { View } from 'react-native'; import PdfView from 'react-native-pdf/PdfView'; export function IosPdfViewer() { const [pages, setPages] = React.useState(0); return ( { setPages(numberOfPages); console.log(`Loaded: ${size.width}x${size.height}pt`); }} onPageChanged={(page, total) => { console.log(`Page ${page}/${total}`); }} onError={(error) => { console.error('PDF Error:', error); }} fitPolicy={2} spacing={10} /> ); } ``` -------------------------------- ### iOS Installation for React Native 0.59 and below Source: https://github.com/wonday/react-native-pdf/blob/master/README.md For older versions of React Native (0.59 and below), you need to manually link the packages using 'react-native link'. ```bash react-native link react-native-blob-util react-native link react-native-pdf ``` -------------------------------- ### Load PDF File and Get Metadata Source: https://github.com/wonday/react-native-pdf/blob/master/_autodocs/api-reference/PdfManager.md Use this method to load a PDF file from a given path and retrieve its metadata. It requires an absolute file path and optionally accepts a password for encrypted PDFs. The returned promise resolves with the internal file number, total number of pages, and the width and height of the first page in PDF points. ```javascript import PdfManager from 'react-native-pdf/PdfManager'; async function loadPdf() { try { const [fileNo, pages, width, height] = await PdfManager.loadFile( '/storage/emulated/0/Download/document.pdf', 'password123' ); console.log(`Loaded PDF: ${pages} pages, ${width}x${height} points`); console.log(`Internal ID: ${fileNo}`); } catch (error) { console.error('Failed to load PDF:', error); } } ``` -------------------------------- ### PinchZoomView _handleStartShouldSetPanResponder Source: https://github.com/wonday/react-native-pdf/blob/master/_autodocs/api-reference/PinchZoomView.md This method determines whether the component should become the responder when a touch starts. It returns false to ignore single-touch events, ensuring only multi-touch gestures trigger zoom. ```typescript _handleStartShouldSetPanResponder(e: GestureResponderEvent, gestureState: PanResponderGestureState): boolean Returns false to not intercept single-touch events. ``` -------------------------------- ### Example Usage of Type Guards Source: https://github.com/wonday/react-native-pdf/blob/master/_autodocs/types.md Demonstrates how to use the type guard functions within an event handler to conditionally access properties based on the event type. This ensures type safety. ```typescript onTextSelectionChange={(event) => { if (isSelectionChanged(event)) { const text = event.nativeEvent.text; // TypeScript knows text exists } }} ``` -------------------------------- ### PDF Viewer with Page Navigation in React Native Source: https://github.com/wonday/react-native-pdf/blob/master/_autodocs/INDEX.md Implement PDF page navigation using `useRef` to control the PDF component. This example includes 'Previous' and 'Next' buttons with state management for the current and total page numbers. ```javascript import React, { useRef } from 'react'; import { Button, View, Text } from 'react-native'; import Pdf from 'react-native-pdf'; export function PdfWithNavigation() { const pdfRef = useRef(null); const [currentPage, setCurrentPage] = React.useState(1); const [totalPages, setTotalPages] = React.useState(0); return (