The PDF SDK is designed to work seamlessly in Node.js environments for server-side processing.
Ensure you have the package installed:
npm install @avanquest/pdf-web-viewer
Here is a complete example of a server-side function that processes a PDF.
import { PdfSdk } from '@avanquest/pdf-web-viewer/sdk';
import fs from 'fs';
async function processServerSidePDF() {
/* Initialize SDK */
await PdfSdk.initialize({
license: process.env.PDF_LICENSE_KEY,
workerPath: '/path/to/pdfworker.js',
});
try {
/* Read file from disk */
const filePath = '/uploads/document.pdf';
const buffer = fs.readFileSync(filePath);
/* Create File object (Node.js 20+ or polyfill required) */
const file = new File([buffer], 'document.pdf', { type: 'application/pdf' });
/* Open document */
const document = await PdfSdk.openDocument({ file });
console.log(`Processing document: ${document.id}`);
/* Perform operations */
await document.deletePages({
range: [0] /* Remove the first page */,
});
/* Save the result */
const result = await document.exportDocument({
as: 'uint8array',
});
/* Clean up */
await document.dispose();
return result;
} catch (error) {
console.error('Server-side processing failed:', error);
throw error;
}
}
workerPath points to a valid location where the worker script can be accessed or loaded.closeFiles to prevent memory leaks on the server.