Use legacy PDF worker for lower Safari versions

This commit is contained in:
Richard Roberson 2025-03-04 13:37:41 -07:00
parent 652a89790d
commit 3d044c9bba
5 changed files with 62211 additions and 296 deletions

548
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
{
"name": "openreader-webui",
"version": "0.2.4",
"version": "0.2.4-patch.1",
"private": true,
"scripts": {
"dev": "next dev --turbopack -p 3003",
@ -16,19 +16,19 @@
"compromise": "^14.14.4",
"howler": "^2.2.4",
"lru-cache": "^11.0.2",
"next": "15.1.5",
"openai": "^4.79.1",
"next": "15.2.1",
"openai": "^4.86.1",
"pdfjs-dist": "4.8.69",
"react": "^19.0.0",
"react-dnd": "^16.0.1",
"react-dnd-html5-backend": "^16.0.1",
"react-dom": "^19.0.0",
"react-dropzone": "^14.3.5",
"react-hot-toast": "^2.5.1",
"react-dropzone": "^14.3.8",
"react-hot-toast": "^2.5.2",
"react-pdf": "^9.2.1",
"react-reader": "^2.0.12",
"string-similarity": "^4.0.4",
"uuid": "^11.0.5"
"uuid": "^11.1.0"
},
"devDependencies": {
"@eslint/eslintrc": "^3",

61896
public/pdf.legacy.worker.mjs Normal file

File diff suppressed because one or more lines are too long

View file

@ -5,10 +5,13 @@ import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const pdfjsDistPath = path.dirname(require.resolve('pdfjs-dist/package.json'));
const pdfWorkerPath = path.join(pdfjsDistPath, 'build', 'pdf.worker.mjs');
const pdfLegacyWorkerPath = path.join(pdfjsDistPath, 'legacy', 'build', 'pdf.worker.mjs');
// Create public directory if it doesn't exist
if (!fs.existsSync('./public')) {
fs.mkdirSync('./public', { recursive: true });
}
// Copy both modern and legacy workers
fs.cpSync(pdfWorkerPath, './public/pdf.worker.mjs', { recursive: true });
fs.cpSync(pdfLegacyWorkerPath, './public/pdf.legacy.worker.mjs', { recursive: true });

View file

@ -4,9 +4,46 @@ import stringSimilarity from 'string-similarity';
import type { TextItem } from 'pdfjs-dist/types/src/display/api';
import type { PDFDocumentProxy } from 'pdfjs-dist';
// Set worker from public directory and compatibility mode
pdfjs.GlobalWorkerOptions.workerSrc = '/pdf.worker.mjs';
pdfjs.GlobalWorkerOptions.workerPort = null;
// Function to detect if we need to use legacy build
function shouldUseLegacyBuild() {
try {
if (typeof window === 'undefined') return false;
const ua = window.navigator.userAgent;
const isSafari = /^((?!chrome|android).)*safari/i.test(ua);
console.log(isSafari ? 'Running on Safari' : 'Not running on Safari');
if (!isSafari) return false;
// Extract Safari version - matches "Version/18" format
const match = ua.match(/Version\/(\d+)/i);
console.log('Safari version:', match);
if (!match || !match[1]) return true; // If we can't determine version, use legacy to be safe
const version = parseInt(match[1]);
return version < 18; // Use legacy build for Safari versions equal or below 18
} catch (e) {
console.error('Error detecting Safari version:', e);
return false;
}
}
// Initialize PDF.js worker
function initPDFWorker() {
try {
if (typeof window !== 'undefined') {
const useLegacy = shouldUseLegacyBuild();
const workerSrc = useLegacy ? '/pdf.legacy.worker.mjs' : '/pdf.worker.mjs';
console.log('Setting PDF worker to:', workerSrc);
pdfjs.GlobalWorkerOptions.workerSrc = workerSrc;
}
} catch (e) {
console.error('Error setting PDF worker:', e);
}
}
// Initialize the worker
initPDFWorker();
interface TextMatch {
elements: HTMLElement[];
@ -22,6 +59,9 @@ export async function extractTextFromPDF(
margins = { header: 0.07, footer: 0.07, left: 0.07, right: 0.07 }
): Promise<string> {
try {
// Log pdf worker version
//console.log('PDF worker version:', pdfjs.GlobalWorkerOptions.workerSrc);
const page = await pdf.getPage(pageNumber);
const textContent = await page.getTextContent();
@ -61,7 +101,7 @@ export async function extractTextFromPDF(
return item.str.trim().length > 0;
});
console.log('Filtered text items:', textItems);
//console.log('Filtered text items:', textItems);
const tolerance = 2;
const lines: TextItem[][] = [];