A beautiful, interactive PDF flipbook component for Svelte 5+ that provides a realistic page-turning experience using pdfjs-dist
and page-flip
.
Live demo - https://svelte-pdf-flipbook.vercel.app/
pnpm install svelte-pdf-flipbook
pnpm install page-flip pdfjs-dist
This component requires Svelte 5 to work properly.
<script>
import { PDFFlipbook } from 'svelte-pdf-flipbook';
</script>
<PDFFlipbook
pdfUrl="/path/to/your/document.pdf"
width={800}
height={600}
flippingTime={800}
/>
Prop | Type | Default | Description |
---|---|---|---|
pdfUrl |
string |
Required | URL or path to the PDF document |
width |
number |
500 |
Width of the flipbook in pixels |
height |
number |
300 |
Height of the flipbook in pixels |
flippingTime |
number |
1000 |
Page flip animation duration in milliseconds |
onFlip |
(event: CustomEvent<FlipEventData>) => void |
() => {} |
Called when a page is flipped |
onStateChange |
(event: CustomEvent<{ state: string }>) => void |
() => {} |
Called when flipbook state changes |
onLoadingComplete |
(event: CustomEvent<{ pageCount: number }>) => void |
() => {} |
Called when PDF loading completes |
onError |
(event: CustomEvent<{ message: string }>) => void |
() => {} |
Called when an error occurs |
<PDFFlipbook
onFlip={(e) => console.log('Flipped to page:', e.detail.page)}
/>
Event Data:
interface FlipEventData {
page: number; // Current page (0-indexed)
oldPage: number; // Previous page
}
<PDFFlipbook
onStateChange={(e) => console.log('State:', e.detail.state)}
/>
Possible States: flipping
, user_fold
, fold_corner
, read
<PDFFlipbook
onLoadingComplete={(e) => console.log('Total pages:', e.detail.pageCount)}
/>
<PDFFlipbook
onError={(e) => console.error('Error:', e.detail.message)}
/>
The component exposes several methods for programmatic control:
<script>
import { PDFFlipbook } from 'svelte-pdf-flipbook';
let flipbook;
function nextPage() {
flipbook.flipNext();
}
function prevPage() {
flipbook.flipPrev();
}
function goToPage(page) {
flipbook.goToPage(page);
}
</script>
<PDFFlipbook bind:this={flipbook} pdfUrl="/document.pdf" />
<button on:click={nextPage}>Next Page</button>
<button on:click={prevPage}>Previous Page</button>
<button on:click={() => goToPage(5)}>Go to Page 5</button>
flipNext()
- Flip to the next pageflipPrev()
- Flip to the previous page goToPage(pageNumber: number)
- Go to specific page (1-indexed)<PDFFlipbook
pdfUrl="/document.pdf"
class="my-custom-flipbook"
/>
<style>
.my-custom-flipbook {
border: 2px solid #333;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}
.my-custom-flipbook :global(.page-content) {
background: #f8f8f8;
}
</style>
<PDFFlipbook
pdfUrl={pdfUrl}
onFlip={handleFlip}
onStateChange={handleStateChange}
onLoadingComplete={handleLoadingComplete}
onError={handleError}
/>
<script>
function handleFlip(e) {
console.log('Page flipped from', e.detail.oldPage, 'to', e.detail.page);
currentPage = e.detail.page + 1; // Convert to 1-indexed for display
}
function handleStateChange(e) {
isFlipping = e.detail.state === 'flipping';
}
function handleLoadingComplete(e) {
totalPages = e.detail.pageCount;
console.log(`PDF loaded with ${totalPages} pages`);
}
function handleError(e) {
console.error('Flipbook error:', e.detail.message);
error = e.detail.message;
}
</script>
The component provides comprehensive error handling:
{#if error}
<div class="error">
<p>Failed to load PDF: {error}</p>
<button on:click={() => loadPdf()}>Retry</button>
</div>
{:else}
<PDFFlipbook
pdfUrl={pdfUrl}
onError={(e) => error = e.detail.message}
/>
{/if}
PDF fails to load
Page flip not working
page-flip
dependency is installedTypeScript errors
# Install dependencies
pnpm install
# Develop
pnpm run dev
# Build
pnpm run build
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License - see LICENSE file for details.
This library uses:
If you encounter any issues or have questions:
Built with ❤️ using Svelte 5, PDF.js, and PageFlip.