This project demonstrates different methods to load and display PDF files in a web application using Next.js and React.
- PDF.js Viewer: Custom implementation using PDF.js library
- Batch rendering of PDF pages
- Scroll-based loading of additional pages
- Google Docs Viewer: Embedded viewer using Google Docs
- Multiple viewing options: iframe, object, embed, and anchor tags
- PDFIframeViewer: Loads PDF files using the
iframe
tag. - PDFViewer: Loads PDF files using the
a
tag. - PDFObjectViewer: Loads PDF files using the
object
tag. - PDFEmbedViewer: Loads PDF files using the
embed
tag. - PDFJSViewer: Custom viewer using PDF.js with batch rendering and scroll loading.
- GoogleDocsViewer: Embedded viewer using Google Docs.
The global styles for the project are defined in the styles/globals.css
file, using Tailwind CSS for styling.
- Clone this repository.
- Run
npm install
to install all dependencies. - Ensure you have a PDF file named
pdf-viewer.pdf
in thepublic
folder. - Run
npm run dev
to start the development server. - Open your browser and visit
http://localhost:3000
to see the result.
The PDF.js viewer (pages/pdf-js-viewer.tsx
) includes the following features:
- Batch rendering of PDF pages for improved performance
- Scroll-based loading of additional pages
- Customizable batch size for rendering
- Error handling and loading states
The Google Docs viewer (pages/google-docs-viewer.tsx
) provides an alternative method to display PDFs:
- Uses Google Docs' embedded viewer
- Requires a publicly accessible URL for the PDF file
- Simple iframe implementation
- For the Google Docs viewer, ensure your PDF is hosted on a publicly accessible URL.
- The PDF.js viewer works with local files in the
public
folder. - If you encounter CORS issues, consider using a Next.js API route as a proxy for serving PDFs.
- If PDFs are not loading, check the browser console for error messages.
- Ensure the
pdf.worker.min.js
file is correctly placed in thepublic
folder for the PDF.js viewer. - For cross-origin PDF loading, make sure your server allows CORS or use a proxy solution.
- Implement zooming and page navigation controls for the PDF.js viewer
- Add support for password-protected PDFs
- Optimize performance for very large PDF files
We hope this project helps you understand different methods of loading and displaying PDF files in a web application.