mirror of
https://github.com/alam00000/bentopdf.git
synced 2026-11-05 20:02:47 +01:00
- Refactor PDF loading across workflow nodes to use loadPdfDocument utility
- Replaced direct calls to PDFDocument.load with loadPdfDocument in multiple nodes to standardize PDF loading process.
This commit is contained in:
@@ -4,12 +4,9 @@
|
||||
* Requires user to configure Ghostscript URL in WASM Settings.
|
||||
*/
|
||||
|
||||
import {
|
||||
getWasmBaseUrl,
|
||||
fetchWasmFile,
|
||||
isWasmAvailable,
|
||||
} from '../config/wasm-cdn-config.js';
|
||||
import { PDFDocument, PDFDict, PDFName, PDFArray } from 'pdf-lib';
|
||||
import { getWasmBaseUrl, isWasmAvailable } from '../config/wasm-cdn-config.js';
|
||||
import { PDFDict, PDFName, PDFArray } from 'pdf-lib';
|
||||
import { loadPdfDocument } from './load-pdf-document.js';
|
||||
|
||||
interface GhostscriptModule {
|
||||
FS: {
|
||||
@@ -160,7 +157,9 @@ export async function convertToPdfA(
|
||||
);
|
||||
} catch (e) {
|
||||
console.error('[Ghostscript] Failed to setup PDF/A assets:', e);
|
||||
throw new Error('Conversion failed: could not create PDF/A definition');
|
||||
throw new Error('Conversion failed: could not create PDF/A definition', {
|
||||
cause: e,
|
||||
});
|
||||
}
|
||||
|
||||
const args = [
|
||||
@@ -200,7 +199,7 @@ export async function convertToPdfA(
|
||||
exitCode = gs.callMain(args);
|
||||
} catch (e) {
|
||||
console.error('[Ghostscript] Exception:', e);
|
||||
throw new Error(`Ghostscript threw an exception: ${e}`);
|
||||
throw new Error(`Ghostscript threw an exception: ${e}`, { cause: e });
|
||||
}
|
||||
|
||||
console.log('[Ghostscript] Exit code:', exitCode);
|
||||
@@ -208,23 +207,23 @@ export async function convertToPdfA(
|
||||
if (exitCode !== 0) {
|
||||
try {
|
||||
gs.FS.unlink(inputPath);
|
||||
} catch {
|
||||
/* ignore */
|
||||
} catch (e) {
|
||||
console.warn('[Ghostscript] Failed to clean up temp file:', e);
|
||||
}
|
||||
try {
|
||||
gs.FS.unlink(outputPath);
|
||||
} catch {
|
||||
/* ignore */
|
||||
} catch (e) {
|
||||
console.warn('[Ghostscript] Failed to clean up temp file:', e);
|
||||
}
|
||||
try {
|
||||
gs.FS.unlink(iccPath);
|
||||
} catch {
|
||||
/* ignore */
|
||||
} catch (e) {
|
||||
console.warn('[Ghostscript] Failed to clean up temp file:', e);
|
||||
}
|
||||
try {
|
||||
gs.FS.unlink(pdfaDefPath);
|
||||
} catch {
|
||||
/* ignore */
|
||||
} catch (e) {
|
||||
console.warn('[Ghostscript] Failed to clean up temp file:', e);
|
||||
}
|
||||
throw new Error(`Ghostscript conversion failed with exit code ${exitCode}`);
|
||||
}
|
||||
@@ -237,29 +236,29 @@ export async function convertToPdfA(
|
||||
output = gs.FS.readFile(outputPath);
|
||||
} catch (e) {
|
||||
console.error('[Ghostscript] Failed to read output:', e);
|
||||
throw new Error('Ghostscript did not produce output file');
|
||||
throw new Error('Ghostscript did not produce output file', { cause: e });
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
try {
|
||||
gs.FS.unlink(inputPath);
|
||||
} catch {
|
||||
/* ignore */
|
||||
} catch (e) {
|
||||
console.warn('[Ghostscript] Failed to clean up temp file:', e);
|
||||
}
|
||||
try {
|
||||
gs.FS.unlink(outputPath);
|
||||
} catch {
|
||||
/* ignore */
|
||||
} catch (e) {
|
||||
console.warn('[Ghostscript] Failed to clean up temp file:', e);
|
||||
}
|
||||
try {
|
||||
gs.FS.unlink(iccPath);
|
||||
} catch {
|
||||
/* ignore */
|
||||
} catch (e) {
|
||||
console.warn('[Ghostscript] Failed to clean up temp file:', e);
|
||||
}
|
||||
try {
|
||||
gs.FS.unlink(pdfaDefPath);
|
||||
} catch {
|
||||
/* ignore */
|
||||
} catch (e) {
|
||||
console.warn('[Ghostscript] Failed to clean up temp file:', e);
|
||||
}
|
||||
|
||||
if (level !== 'PDF/A-1b') {
|
||||
@@ -282,8 +281,7 @@ export async function convertToPdfA(
|
||||
async function addPageGroupDictionaries(
|
||||
pdfData: Uint8Array
|
||||
): Promise<Uint8Array> {
|
||||
const pdfDoc = await PDFDocument.load(pdfData, {
|
||||
ignoreEncryption: true,
|
||||
const pdfDoc = await loadPdfDocument(pdfData, {
|
||||
updateMetadata: false,
|
||||
});
|
||||
|
||||
@@ -352,7 +350,7 @@ async function addPageGroupDictionaries(
|
||||
}
|
||||
|
||||
if (iccProfileRef) {
|
||||
pdfDoc.context.enumerateIndirectObjects().forEach(([ref, obj]) => {
|
||||
pdfDoc.context.enumerateIndirectObjects().forEach(([_ref, obj]) => {
|
||||
if (
|
||||
obj instanceof PDFDict ||
|
||||
(obj && typeof obj === 'object' && 'dict' in obj)
|
||||
@@ -437,17 +435,23 @@ export async function convertFontsToOutlines(
|
||||
} catch (e) {
|
||||
try {
|
||||
gs.FS.unlink(inputPath);
|
||||
} catch {}
|
||||
throw new Error(`Ghostscript threw an exception: ${e}`);
|
||||
} catch (e2) {
|
||||
console.warn('[Ghostscript] Failed to clean up temp file:', e2);
|
||||
}
|
||||
throw new Error(`Ghostscript threw an exception: ${e}`, { cause: e });
|
||||
}
|
||||
|
||||
if (exitCode !== 0) {
|
||||
try {
|
||||
gs.FS.unlink(inputPath);
|
||||
} catch {}
|
||||
} catch (e) {
|
||||
console.warn('[Ghostscript] Failed to clean up temp file:', e);
|
||||
}
|
||||
try {
|
||||
gs.FS.unlink(outputPath);
|
||||
} catch {}
|
||||
} catch (e) {
|
||||
console.warn('[Ghostscript] Failed to clean up temp file:', e);
|
||||
}
|
||||
throw new Error(`Ghostscript conversion failed with exit code ${exitCode}`);
|
||||
}
|
||||
|
||||
@@ -455,15 +459,19 @@ export async function convertFontsToOutlines(
|
||||
try {
|
||||
output = gs.FS.readFile(outputPath);
|
||||
} catch (e) {
|
||||
throw new Error('Ghostscript did not produce output file');
|
||||
throw new Error('Ghostscript did not produce output file', { cause: e });
|
||||
}
|
||||
|
||||
try {
|
||||
gs.FS.unlink(inputPath);
|
||||
} catch {}
|
||||
} catch (e) {
|
||||
console.warn('[Ghostscript] Failed to clean up temp file:', e);
|
||||
}
|
||||
try {
|
||||
gs.FS.unlink(outputPath);
|
||||
} catch {}
|
||||
} catch (e) {
|
||||
console.warn('[Ghostscript] Failed to clean up temp file:', e);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user