// ES6 Compliant Syntax
// GitHub Copilot - GPT-4 - July 21, 2025
// File: index.js
import WebViewer from '@pdftron/webviewer';
import saveAs from 'file-saver';
const licenseKey = 'YOUR_WEBVIEWER_LICENSE_KEY';
// Customize the WebViewer UI
const customizeUI = (instance) => {
const { UI } = instance;
// Set the toolbar group to the annotate tools
UI.setToolbarGroup('toolbarGroup-Annotate');
// Convert button
const convertButton = new UI.Components.CustomButton({
dataElement: 'convertToPdfButton',
className: 'custom-button-class',
label: 'Image to PDF',
onClick: () => convert(instance), // convert the image to PDF
style: {
padding: '10px 10px',
backgroundColor: 'blue',
color: 'white',
}
});
const defaultHeader = UI.getModularHeader('default-top-header');
defaultHeader.setItems([...defaultHeader.items, convertButton]);
};
// convert the image to PDF
const convert = async (instance) => {
// Get the document
const document = instance.Core.documentViewer.getDocument();
// Get the filename
let fileName = document.getFilename();
// Ensure it ends with .pdf. If not, replace it with .pdf extension
if (!fileName.endsWith('.pdf'))
fileName = fileName.replace(/\.[^/.]+$/, '') + '.pdf';
try {
const data = await document.getFileData({
downloadType: 'pdf',
});
const arr = new Uint8Array(data);
const blob = new Blob([arr], { type: 'application/pdf' });
// Save the image as a PDF
saveAs(blob, fileName);
} catch (e) {
console.error('❌ Conversion failed:', e);
}
};
WebViewer(
{
path: '/lib',
initialDoc: 'https://apryse.s3.us-west-1.amazonaws.com/public/files/samples/Vancouver_komal-brar.jpg',
enableFilePicker: true, // Enable file picker to open files. In WebViewer -> menu icon -> Open File
licenseKey: licenseKey,
},
document.getElementById('viewer')
).then((instance) => {
// customize WebViewer UI
customizeUI(instance);
console.log('✅ WebViewer loaded successfully.');
}).catch((error) => {
console.error('❌ Failed to initialize WebViewer:', error);
});