mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-11-14 17:14:02 +01:00
refactor: and improve code readability in PDF to EPUB conversion
This commit is contained in:
@@ -15,18 +15,16 @@ export default function PdfToEpub({ title }: ToolComponentProps) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
setIsProcessing(true);
|
setIsProcessing(true);
|
||||||
setResult(null); // Clear previous result
|
setResult(null);
|
||||||
const epub = await convertPdfToEpub(files[0]);
|
const epub = await convertPdfToEpub(files[0]);
|
||||||
setResult(epub);
|
setResult(epub);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to convert PDF to EPUB:', error);
|
console.error('Failed to convert PDF to EPUB:', error);
|
||||||
// Handle error appropriately - maybe set an error state
|
|
||||||
} finally {
|
} finally {
|
||||||
setIsProcessing(false);
|
setIsProcessing(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Auto-trigger conversion when file is uploaded
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (input) {
|
if (input) {
|
||||||
compute([input]);
|
compute([input]);
|
||||||
|
|||||||
@@ -5,9 +5,6 @@ import JSZip from 'jszip';
|
|||||||
// Set worker source for PDF.js
|
// Set worker source for PDF.js
|
||||||
pdfjsLib.GlobalWorkerOptions.workerSrc = pdfjsWorker;
|
pdfjsLib.GlobalWorkerOptions.workerSrc = pdfjsWorker;
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper to convert raw text into clean paragraphs
|
|
||||||
*/
|
|
||||||
function formatTextToParagraphs(raw: string): string {
|
function formatTextToParagraphs(raw: string): string {
|
||||||
return raw
|
return raw
|
||||||
.split(/\n{2,}|\r{2,}/g) // Split on double line breaks
|
.split(/\n{2,}|\r{2,}/g) // Split on double line breaks
|
||||||
@@ -20,12 +17,11 @@ function formatTextToParagraphs(raw: string): string {
|
|||||||
export async function convertPdfToEpub(pdfFile: File): Promise<File> {
|
export async function convertPdfToEpub(pdfFile: File): Promise<File> {
|
||||||
const arrayBuffer = await pdfFile.arrayBuffer();
|
const arrayBuffer = await pdfFile.arrayBuffer();
|
||||||
|
|
||||||
// Load PDF document
|
|
||||||
const loadingTask = pdfjsLib.getDocument({ data: arrayBuffer });
|
const loadingTask = pdfjsLib.getDocument({ data: arrayBuffer });
|
||||||
const pdfDoc = await loadingTask.promise;
|
const pdfDoc = await loadingTask.promise;
|
||||||
const numPages = pdfDoc.numPages;
|
const numPages = pdfDoc.numPages;
|
||||||
|
|
||||||
// Extract text from all pages
|
// Extracting text
|
||||||
const pages: string[] = [];
|
const pages: string[] = [];
|
||||||
for (let i = 1; i <= numPages; i++) {
|
for (let i = 1; i <= numPages; i++) {
|
||||||
const page = await pdfDoc.getPage(i);
|
const page = await pdfDoc.getPage(i);
|
||||||
@@ -36,7 +32,6 @@ export async function convertPdfToEpub(pdfFile: File): Promise<File> {
|
|||||||
pages.push(pageText);
|
pages.push(pageText);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create EPUB structure using JSZip
|
|
||||||
const zip = new JSZip();
|
const zip = new JSZip();
|
||||||
|
|
||||||
zip.file('mimetype', 'application/epub+zip', { compression: 'STORE' });
|
zip.file('mimetype', 'application/epub+zip', { compression: 'STORE' });
|
||||||
|
|||||||
Reference in New Issue
Block a user