feat(toc): implement table of contents generation feature

- Added a new page for generating a table of contents from PDF bookmarks.
- Introduced a web worker to handle the TOC generation process in the background.
- Updated TypeScript definitions for coherentpdf integration.
- Enhanced the Vite configuration to include necessary headers and exclude specific dependencies.
- Modified tsconfig to include WebWorker library and updated file inclusion paths.
- Added a new script for handling TOC logic and user interactions.
This commit is contained in:
abdullahalam123
2025-11-08 12:37:10 +05:30
parent 9befab7758
commit 661c030ae1
9 changed files with 716 additions and 3 deletions

10
public/coherentpdf.browser.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,22 @@
declare const coherentpdf: typeof import("../../src/types/coherentpdf.global").coherentpdf;
interface GenerateTOCMessage {
command: 'generate-toc';
pdfData: ArrayBuffer;
title: string;
fontSize: number;
fontFamily: number;
addBookmark: boolean;
}
interface TOCSuccessResponse {
status: 'success';
pdfBytes: ArrayBuffer;
}
interface TOCErrorResponse {
status: 'error';
message: string;
}
type TOCResponse = TOCSuccessResponse | TOCErrorResponse;

View File

@@ -0,0 +1,57 @@
self.importScripts('/coherentpdf.browser.min.js');
function generateTableOfContentsInWorker(
pdfData,
title,
fontSize,
fontFamily,
addBookmark
) {
try {
const uint8Array = new Uint8Array(pdfData);
const pdf = coherentpdf.fromMemory(uint8Array, '');
coherentpdf.startGetBookmarkInfo(pdf);
const bookmarkCount = coherentpdf.numberBookmarks();
coherentpdf.endGetBookmarkInfo();
if (bookmarkCount === 0) {
coherentpdf.deletePdf(pdf);
self.postMessage({
status: 'error',
message: 'This PDF does not have any bookmarks. Please add bookmarks first using the Bookmark tool.',
});
return;
}
coherentpdf.tableOfContents(pdf, fontFamily, fontSize, title, addBookmark);
const outputBytes = coherentpdf.toMemory(pdf, false, false);
const outputBytesBuffer = outputBytes.buffer;
coherentpdf.deletePdf(pdf);
self.postMessage(
{
status: 'success',
pdfBytes: outputBytesBuffer,
},
[outputBytesBuffer]
);
} catch (error) {
self.postMessage({
status: 'error',
message: error instanceof Error ? error.message : 'Unknown error occurred during table of contents generation.',
});
}
}
self.onmessage = (e) => {
if (e.data.command === 'generate-toc') {
generateTableOfContentsInWorker(
e.data.pdfData,
e.data.title,
e.data.fontSize,
e.data.fontFamily,
e.data.addBookmark
);
}
};