mirror of
https://github.com/alam00000/bentopdf.git
synced 2026-11-13 17:54:19 +01:00
feat: separate AGPL libraries and add dynamic WASM loading
- Add WASM settings page for configuring external AGPL modules - Implement dynamic loading for PyMuPDF, Ghostscript, and CoherentPDF - Add Cloudflare Worker proxy for serving WASM files with CORS - Update all affected tool pages to check WASM availability - Add showWasmRequiredDialog for missing module configuration Documentation: - Update README, licensing.html, and docs to clarify AGPL components are not bundled and must be configured separately - Add WASM-PROXY.md deployment guide with recommended source URLs - Rename "CPDF" to "CoherentPDF" for consistency
This commit is contained in:
1 parent
f6d432eaa7
commit
2c85ca74e9
75 files changed
+9696
-6587
No files matched your search
+214
-195
@@ -2,240 +2,259 @@ import { showLoader, hideLoader, showAlert } from '../ui.js';
|
||||
import { downloadFile, formatBytes } from '../utils/helpers.js';
|
||||
import { createIcons, icons } from 'lucide';
|
||||
import JSZip from 'jszip';
|
||||
import { PyMuPDF } from '@bentopdf/pymupdf-wasm';
|
||||
import { getWasmBaseUrl } from '../config/wasm-cdn-config.js';
|
||||
|
||||
const pymupdf = new PyMuPDF(getWasmBaseUrl('pymupdf'));
|
||||
import { isWasmAvailable, getWasmBaseUrl } from '../config/wasm-cdn-config.js';
|
||||
import { showWasmRequiredDialog } from '../utils/wasm-provider.js';
|
||||
import { loadPyMuPDF, isPyMuPDFAvailable } from '../utils/pymupdf-loader.js';
|
||||
let file: File | null = null;
|
||||
|
||||
const updateUI = () => {
|
||||
const fileDisplayArea = document.getElementById('file-display-area');
|
||||
const optionsPanel = document.getElementById('options-panel');
|
||||
const fileDisplayArea = document.getElementById('file-display-area');
|
||||
const optionsPanel = document.getElementById('options-panel');
|
||||
|
||||
if (!fileDisplayArea || !optionsPanel) return;
|
||||
if (!fileDisplayArea || !optionsPanel) return;
|
||||
|
||||
fileDisplayArea.innerHTML = '';
|
||||
fileDisplayArea.innerHTML = '';
|
||||
|
||||
if (file) {
|
||||
optionsPanel.classList.remove('hidden');
|
||||
if (file) {
|
||||
optionsPanel.classList.remove('hidden');
|
||||
|
||||
const fileDiv = document.createElement('div');
|
||||
fileDiv.className = 'flex items-center justify-between bg-gray-700 p-3 rounded-lg text-sm';
|
||||
const fileDiv = document.createElement('div');
|
||||
fileDiv.className =
|
||||
'flex items-center justify-between bg-gray-700 p-3 rounded-lg text-sm';
|
||||
|
||||
const infoContainer = document.createElement('div');
|
||||
infoContainer.className = 'flex flex-col overflow-hidden';
|
||||
const infoContainer = document.createElement('div');
|
||||
infoContainer.className = 'flex flex-col overflow-hidden';
|
||||
|
||||
const nameSpan = document.createElement('div');
|
||||
nameSpan.className = 'truncate font-medium text-gray-200 text-sm mb-1';
|
||||
nameSpan.textContent = file.name;
|
||||
const nameSpan = document.createElement('div');
|
||||
nameSpan.className = 'truncate font-medium text-gray-200 text-sm mb-1';
|
||||
nameSpan.textContent = file.name;
|
||||
|
||||
const metaSpan = document.createElement('div');
|
||||
metaSpan.className = 'text-xs text-gray-400';
|
||||
metaSpan.textContent = formatBytes(file.size);
|
||||
const metaSpan = document.createElement('div');
|
||||
metaSpan.className = 'text-xs text-gray-400';
|
||||
metaSpan.textContent = formatBytes(file.size);
|
||||
|
||||
infoContainer.append(nameSpan, metaSpan);
|
||||
infoContainer.append(nameSpan, metaSpan);
|
||||
|
||||
const removeBtn = document.createElement('button');
|
||||
removeBtn.className = 'ml-4 text-red-400 hover:text-red-300 flex-shrink-0';
|
||||
removeBtn.innerHTML = '<i data-lucide="trash-2" class="w-4 h-4"></i>';
|
||||
removeBtn.onclick = resetState;
|
||||
const removeBtn = document.createElement('button');
|
||||
removeBtn.className = 'ml-4 text-red-400 hover:text-red-300 flex-shrink-0';
|
||||
removeBtn.innerHTML = '<i data-lucide="trash-2" class="w-4 h-4"></i>';
|
||||
removeBtn.onclick = resetState;
|
||||
|
||||
fileDiv.append(infoContainer, removeBtn);
|
||||
fileDisplayArea.appendChild(fileDiv);
|
||||
fileDiv.append(infoContainer, removeBtn);
|
||||
fileDisplayArea.appendChild(fileDiv);
|
||||
|
||||
createIcons({ icons });
|
||||
} else {
|
||||
optionsPanel.classList.add('hidden');
|
||||
}
|
||||
createIcons({ icons });
|
||||
} else {
|
||||
optionsPanel.classList.add('hidden');
|
||||
}
|
||||
};
|
||||
|
||||
const resetState = () => {
|
||||
file = null;
|
||||
const fileInput = document.getElementById('file-input') as HTMLInputElement;
|
||||
if (fileInput) fileInput.value = '';
|
||||
updateUI();
|
||||
file = null;
|
||||
const fileInput = document.getElementById('file-input') as HTMLInputElement;
|
||||
if (fileInput) fileInput.value = '';
|
||||
updateUI();
|
||||
};
|
||||
|
||||
function tableToCsv(rows: (string | null)[][]): string {
|
||||
return rows.map(row =>
|
||||
row.map(cell => {
|
||||
const cellStr = cell ?? '';
|
||||
if (cellStr.includes(',') || cellStr.includes('"') || cellStr.includes('\n')) {
|
||||
return `"${cellStr.replace(/"/g, '""')}"`;
|
||||
}
|
||||
return cellStr;
|
||||
}).join(',')
|
||||
).join('\n');
|
||||
return rows
|
||||
.map((row) =>
|
||||
row
|
||||
.map((cell) => {
|
||||
const cellStr = cell ?? '';
|
||||
if (
|
||||
cellStr.includes(',') ||
|
||||
cellStr.includes('"') ||
|
||||
cellStr.includes('\n')
|
||||
) {
|
||||
return `"${cellStr.replace(/"/g, '""')}"`;
|
||||
}
|
||||
return cellStr;
|
||||
})
|
||||
.join(',')
|
||||
)
|
||||
.join('\n');
|
||||
}
|
||||
|
||||
async function extract() {
|
||||
if (!file) {
|
||||
showAlert('No File', 'Please upload a PDF file first.');
|
||||
return;
|
||||
if (!file) {
|
||||
showAlert('No File', 'Please upload a PDF file first.');
|
||||
return;
|
||||
}
|
||||
|
||||
const formatRadios = document.querySelectorAll('input[name="export-format"]');
|
||||
let format = 'csv';
|
||||
formatRadios.forEach((radio: Element) => {
|
||||
if ((radio as HTMLInputElement).checked) {
|
||||
format = (radio as HTMLInputElement).value;
|
||||
}
|
||||
});
|
||||
|
||||
const formatRadios = document.querySelectorAll('input[name="export-format"]');
|
||||
let format = 'csv';
|
||||
formatRadios.forEach((radio: Element) => {
|
||||
if ((radio as HTMLInputElement).checked) {
|
||||
format = (radio as HTMLInputElement).value;
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
showLoader('Loading Engine...');
|
||||
const pymupdf = await loadPyMuPDF();
|
||||
showLoader('Extracting tables...');
|
||||
|
||||
try {
|
||||
await pymupdf.load();
|
||||
showLoader('Extracting tables...');
|
||||
const doc = await pymupdf.open(file);
|
||||
const pageCount = doc.pageCount;
|
||||
const baseName = file.name.replace(/\.[^/.]+$/, '');
|
||||
|
||||
const doc = await pymupdf.open(file);
|
||||
const pageCount = doc.pageCount;
|
||||
const baseName = file.name.replace(/\.[^/.]+$/, '');
|
||||
|
||||
interface TableData {
|
||||
page: number;
|
||||
tableIndex: number;
|
||||
rows: (string | null)[][];
|
||||
markdown: string;
|
||||
rowCount: number;
|
||||
colCount: number;
|
||||
}
|
||||
|
||||
const allTables: TableData[] = [];
|
||||
|
||||
for (let i = 0; i < pageCount; i++) {
|
||||
showLoader(`Scanning page ${i + 1} of ${pageCount}...`);
|
||||
const page = doc.getPage(i);
|
||||
const tables = page.findTables();
|
||||
|
||||
tables.forEach((table, tableIdx) => {
|
||||
allTables.push({
|
||||
page: i + 1,
|
||||
tableIndex: tableIdx + 1,
|
||||
rows: table.rows,
|
||||
markdown: table.markdown,
|
||||
rowCount: table.rowCount,
|
||||
colCount: table.colCount
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if (allTables.length === 0) {
|
||||
showAlert('No Tables Found', 'No tables were detected in this PDF.');
|
||||
return;
|
||||
}
|
||||
|
||||
if (allTables.length === 1) {
|
||||
const table = allTables[0];
|
||||
let content: string;
|
||||
let ext: string;
|
||||
let mimeType: string;
|
||||
|
||||
if (format === 'csv') {
|
||||
content = tableToCsv(table.rows);
|
||||
ext = 'csv';
|
||||
mimeType = 'text/csv';
|
||||
} else if (format === 'json') {
|
||||
content = JSON.stringify(table.rows, null, 2);
|
||||
ext = 'json';
|
||||
mimeType = 'application/json';
|
||||
} else {
|
||||
content = table.markdown;
|
||||
ext = 'md';
|
||||
mimeType = 'text/markdown';
|
||||
}
|
||||
|
||||
const blob = new Blob([content], { type: mimeType });
|
||||
downloadFile(blob, `${baseName}_table.${ext}`);
|
||||
showAlert('Success', `Extracted 1 table successfully!`, 'success', resetState);
|
||||
} else {
|
||||
showLoader('Creating ZIP file...');
|
||||
const zip = new JSZip();
|
||||
|
||||
allTables.forEach((table, idx) => {
|
||||
const filename = `table_${idx + 1}_page${table.page}`;
|
||||
let content: string;
|
||||
let ext: string;
|
||||
|
||||
if (format === 'csv') {
|
||||
content = tableToCsv(table.rows);
|
||||
ext = 'csv';
|
||||
} else if (format === 'json') {
|
||||
content = JSON.stringify(table.rows, null, 2);
|
||||
ext = 'json';
|
||||
} else {
|
||||
content = table.markdown;
|
||||
ext = 'md';
|
||||
}
|
||||
|
||||
zip.file(`${filename}.${ext}`, content);
|
||||
});
|
||||
|
||||
const zipBlob = await zip.generateAsync({ type: 'blob' });
|
||||
downloadFile(zipBlob, `${baseName}_tables.zip`);
|
||||
showAlert('Success', `Extracted ${allTables.length} tables successfully!`, 'success', resetState);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
const message = e instanceof Error ? e.message : 'Unknown error';
|
||||
showAlert('Error', `Failed to extract tables. ${message}`);
|
||||
} finally {
|
||||
hideLoader();
|
||||
interface TableData {
|
||||
page: number;
|
||||
tableIndex: number;
|
||||
rows: (string | null)[][];
|
||||
markdown: string;
|
||||
rowCount: number;
|
||||
colCount: number;
|
||||
}
|
||||
|
||||
const allTables: TableData[] = [];
|
||||
|
||||
for (let i = 0; i < pageCount; i++) {
|
||||
showLoader(`Scanning page ${i + 1} of ${pageCount}...`);
|
||||
const page = doc.getPage(i);
|
||||
const tables = page.findTables();
|
||||
|
||||
tables.forEach((table, tableIdx) => {
|
||||
allTables.push({
|
||||
page: i + 1,
|
||||
tableIndex: tableIdx + 1,
|
||||
rows: table.rows,
|
||||
markdown: table.markdown,
|
||||
rowCount: table.rowCount,
|
||||
colCount: table.colCount,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if (allTables.length === 0) {
|
||||
showAlert('No Tables Found', 'No tables were detected in this PDF.');
|
||||
return;
|
||||
}
|
||||
|
||||
if (allTables.length === 1) {
|
||||
const table = allTables[0];
|
||||
let content: string;
|
||||
let ext: string;
|
||||
let mimeType: string;
|
||||
|
||||
if (format === 'csv') {
|
||||
content = tableToCsv(table.rows);
|
||||
ext = 'csv';
|
||||
mimeType = 'text/csv';
|
||||
} else if (format === 'json') {
|
||||
content = JSON.stringify(table.rows, null, 2);
|
||||
ext = 'json';
|
||||
mimeType = 'application/json';
|
||||
} else {
|
||||
content = table.markdown;
|
||||
ext = 'md';
|
||||
mimeType = 'text/markdown';
|
||||
}
|
||||
|
||||
const blob = new Blob([content], { type: mimeType });
|
||||
downloadFile(blob, `${baseName}_table.${ext}`);
|
||||
showAlert(
|
||||
'Success',
|
||||
`Extracted 1 table successfully!`,
|
||||
'success',
|
||||
resetState
|
||||
);
|
||||
} else {
|
||||
showLoader('Creating ZIP file...');
|
||||
const zip = new JSZip();
|
||||
|
||||
allTables.forEach((table, idx) => {
|
||||
const filename = `table_${idx + 1}_page${table.page}`;
|
||||
let content: string;
|
||||
let ext: string;
|
||||
|
||||
if (format === 'csv') {
|
||||
content = tableToCsv(table.rows);
|
||||
ext = 'csv';
|
||||
} else if (format === 'json') {
|
||||
content = JSON.stringify(table.rows, null, 2);
|
||||
ext = 'json';
|
||||
} else {
|
||||
content = table.markdown;
|
||||
ext = 'md';
|
||||
}
|
||||
|
||||
zip.file(`${filename}.${ext}`, content);
|
||||
});
|
||||
|
||||
const zipBlob = await zip.generateAsync({ type: 'blob' });
|
||||
downloadFile(zipBlob, `${baseName}_tables.zip`);
|
||||
showAlert(
|
||||
'Success',
|
||||
`Extracted ${allTables.length} tables successfully!`,
|
||||
'success',
|
||||
resetState
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
const message = e instanceof Error ? e.message : 'Unknown error';
|
||||
showAlert('Error', `Failed to extract tables. ${message}`);
|
||||
} finally {
|
||||
hideLoader();
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const fileInput = document.getElementById('file-input') as HTMLInputElement;
|
||||
const dropZone = document.getElementById('drop-zone');
|
||||
const processBtn = document.getElementById('process-btn');
|
||||
const backBtn = document.getElementById('back-to-tools');
|
||||
const fileInput = document.getElementById('file-input') as HTMLInputElement;
|
||||
const dropZone = document.getElementById('drop-zone');
|
||||
const processBtn = document.getElementById('process-btn');
|
||||
const backBtn = document.getElementById('back-to-tools');
|
||||
|
||||
if (backBtn) {
|
||||
backBtn.addEventListener('click', () => {
|
||||
window.location.href = import.meta.env.BASE_URL;
|
||||
});
|
||||
if (backBtn) {
|
||||
backBtn.addEventListener('click', () => {
|
||||
window.location.href = import.meta.env.BASE_URL;
|
||||
});
|
||||
}
|
||||
|
||||
const handleFileSelect = (newFiles: FileList | null) => {
|
||||
if (!newFiles || newFiles.length === 0) return;
|
||||
const validFile = Array.from(newFiles).find(
|
||||
(f) => f.type === 'application/pdf'
|
||||
);
|
||||
|
||||
if (!validFile) {
|
||||
showAlert('Invalid File', 'Please upload a PDF file.');
|
||||
return;
|
||||
}
|
||||
|
||||
const handleFileSelect = (newFiles: FileList | null) => {
|
||||
if (!newFiles || newFiles.length === 0) return;
|
||||
const validFile = Array.from(newFiles).find(f => f.type === 'application/pdf');
|
||||
file = validFile;
|
||||
updateUI();
|
||||
};
|
||||
|
||||
if (!validFile) {
|
||||
showAlert('Invalid File', 'Please upload a PDF file.');
|
||||
return;
|
||||
}
|
||||
if (fileInput && dropZone) {
|
||||
fileInput.addEventListener('change', (e) => {
|
||||
handleFileSelect((e.target as HTMLInputElement).files);
|
||||
});
|
||||
|
||||
file = validFile;
|
||||
updateUI();
|
||||
};
|
||||
dropZone.addEventListener('dragover', (e) => {
|
||||
e.preventDefault();
|
||||
dropZone.classList.add('bg-gray-700');
|
||||
});
|
||||
|
||||
if (fileInput && dropZone) {
|
||||
fileInput.addEventListener('change', (e) => {
|
||||
handleFileSelect((e.target as HTMLInputElement).files);
|
||||
});
|
||||
dropZone.addEventListener('dragleave', (e) => {
|
||||
e.preventDefault();
|
||||
dropZone.classList.remove('bg-gray-700');
|
||||
});
|
||||
|
||||
dropZone.addEventListener('dragover', (e) => {
|
||||
e.preventDefault();
|
||||
dropZone.classList.add('bg-gray-700');
|
||||
});
|
||||
dropZone.addEventListener('drop', (e) => {
|
||||
e.preventDefault();
|
||||
dropZone.classList.remove('bg-gray-700');
|
||||
handleFileSelect(e.dataTransfer?.files ?? null);
|
||||
});
|
||||
|
||||
dropZone.addEventListener('dragleave', (e) => {
|
||||
e.preventDefault();
|
||||
dropZone.classList.remove('bg-gray-700');
|
||||
});
|
||||
fileInput.addEventListener('click', () => {
|
||||
fileInput.value = '';
|
||||
});
|
||||
}
|
||||
|
||||
dropZone.addEventListener('drop', (e) => {
|
||||
e.preventDefault();
|
||||
dropZone.classList.remove('bg-gray-700');
|
||||
handleFileSelect(e.dataTransfer?.files ?? null);
|
||||
});
|
||||
|
||||
fileInput.addEventListener('click', () => {
|
||||
fileInput.value = '';
|
||||
});
|
||||
}
|
||||
|
||||
if (processBtn) {
|
||||
processBtn.addEventListener('click', extract);
|
||||
}
|
||||
if (processBtn) {
|
||||
processBtn.addEventListener('click', extract);
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user