|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Document Handling in React Pdfviewer component | Syncfusion |
| 4 | +description: This page helps you to learn about how to Open PDF from URL, Base64, Blob, Stream, Cloud storage in Syncfusion React Pdfviewer component. |
| 5 | +control: PDF Viewer |
| 6 | +platform: document-processing |
| 7 | +documentation: ug |
| 8 | +domainurl: ##DomainURL## |
| 9 | +--- |
| 10 | + |
| 11 | +# Handling Large PDF Files in React PDF Viewer |
| 12 | + |
| 13 | +This article explains why large PDF files require special handling in web applications and provides best practices for loading and displaying them efficiently in the Syncfusion React PDF Viewer. |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## Explanation: Why Large PDFs Need Special Handling |
| 18 | + |
| 19 | +Large PDF files (typically 50MB–100MB or more) can cause high memory usage and performance issues in browsers. Browsers may become unresponsive or crash when attempting to load or render very large documents, especially if inefficient loading methods are used. |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +## How-to Guide: Best Ways to Load Large PDFs |
| 24 | + |
| 25 | +Follow these recommendations to optimize the loading and viewing experience for large PDF files: |
| 26 | + |
| 27 | +### 1. Load from Blob (Recommended) |
| 28 | + |
| 29 | +If you fetch the PDF from a custom API or server, use the Blob approach. This avoids base64 encoding overhead and is more memory-efficient. |
| 30 | + |
| 31 | +```js |
| 32 | +fetch('https://your-api/large-file.pdf') |
| 33 | + .then((response) => response.blob()) |
| 34 | + .then((blob) => { |
| 35 | + const blobUrl = URL.createObjectURL(blob); |
| 36 | + viewer.load(blobUrl, null); // Load using blob URL |
| 37 | + }) |
| 38 | + .catch((error) => { |
| 39 | + console.error('Error loading PDF:', error); |
| 40 | + }); |
| 41 | +``` |
| 42 | + |
| 43 | +### 2. Avoid Base64 for Large Files |
| 44 | + |
| 45 | +Base64 encoding increases file size by ~33% and consumes more memory. For large PDFs, always prefer Blob or direct URL loading over base64. |
| 46 | + |
| 47 | +### 3. Minimize Injected Modules |
| 48 | + |
| 49 | +Reduce the number of injected modules in the PDF Viewer to lower background processing and memory usage. For large files, avoid modules like: |
| 50 | +- Text Search |
| 51 | +- Text Selection |
| 52 | +- Organize Pages |
| 53 | +- Thumbnail View |
| 54 | + |
| 55 | +**Example:** |
| 56 | +```tsx |
| 57 | +<PdfViewerComponent |
| 58 | + id="container" |
| 59 | + documentPath={...} |
| 60 | + serviceUrl={...} |
| 61 | + style={{ height: '640px' }} |
| 62 | +> |
| 63 | + <Inject services={[Toolbar, Magnification, Navigation, Print]} /> |
| 64 | +</PdfViewerComponent> |
| 65 | +``` |
| 66 | + |
| 67 | +### 4. Enable Local Storage for Performance |
| 68 | + |
| 69 | +Enabling local storage in the PDF Viewer can improve performance and smoothness when working with large files. This allows the viewer to cache document data locally, reducing repeated network requests and memory spikes. |
| 70 | + |
| 71 | +Use the `enableLocalStorage` property to control this behavior. When set to `true`, session data is stored in memory for the current session; when `false` (default), browser session storage is used. |
| 72 | + |
| 73 | +**Example:** |
| 74 | +```tsx |
| 75 | +import * as ReactDOM from 'react-dom'; |
| 76 | +import * as React from 'react'; |
| 77 | +import { PdfViewerComponent, Toolbar, Magnification, Navigation, Print, Inject } from '@syncfusion/ej2-react-pdfviewer'; |
| 78 | + |
| 79 | +function App() { |
| 80 | + return ( |
| 81 | + <div className='control-section'> |
| 82 | + <PdfViewerComponent |
| 83 | + id="container" |
| 84 | + documentPath="https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf" |
| 85 | + resourceUrl="https://cdn.syncfusion.com/ej2/31.2.2/dist/ej2-pdfviewer-lib" |
| 86 | + style={{ height: '640px' }} |
| 87 | + enableLocalStorage={true} |
| 88 | + > |
| 89 | + <Inject services={[Toolbar, Magnification, Navigation, Print]} /> |
| 90 | + </PdfViewerComponent> |
| 91 | + </div> |
| 92 | + ); |
| 93 | +} |
| 94 | +const root = ReactDOM.createRoot(document.getElementById('sample')); |
| 95 | +root.render(<App />); |
| 96 | +``` |
| 97 | + |
| 98 | +**Considerations:** |
| 99 | +- Enabling in-memory storage can increase memory usage, especially for large documents or many interactive elements. |
| 100 | +- Dispose the PDF Viewer instance when not needed to avoid memory leaks. |
| 101 | +- The default value is `false` (browser session storage). |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +## Reference |
| 106 | + |
| 107 | +- [React PDF Viewer API Reference](https://ej2.syncfusion.com/react/documentation/api/pdfviewer) |
| 108 | +- [FAQ in Syncfusion React PDF Viewer](https://help.syncfusion.com/document-processing/pdf/pdf-viewer/react/how-to-overview) |
| 109 | + |
| 110 | +--- |
| 111 | + |
| 112 | +## See Also |
| 113 | + |
| 114 | +- [Load PDF (GitHub Sample)](https://github.com/SyncfusionExamples/react-pdf-viewer-examples/tree/master/Save%20and%20Load) |
| 115 | +- [General PDF Viewer Documentation](https://help.syncfusion.com/document-processing/pdf/pdf-viewer/react/getting-started) |
0 commit comments