/*
ES6 Compliant Syntax
GitHub Copilot, Version 1.0, Model GPT-4, 2024-06-09
File: validate-pdfa/index.js
*/
import WebViewer from '@pdftron/webviewer';
const licenseKey = 'YOUR_WEBVIEWER_LICENSE_KEY';
const element = document.getElementById('viewer');
let theInstance = null;
const onLoad = async (instance) => {
theInstance = instance;
};
WebViewer(
{
path: '/lib',
licenseKey: licenseKey,
initialDoc: 'https://apryse.s3.us-west-1.amazonaws.com/public/files/samples/pdfa.pdf',
fullAPI: true, // Enable full API access to use validation features
},
element
).then((instance) => {
onLoad(instance);
});
const pdfaLevels = [
{ label: 'PDF/A-1A', value: '1A' },
{ label: 'PDF/A-1B', value: '1B' },
{ label: 'PDF/A-2A', value: '2A' },
{ label: 'PDF/A-2B', value: '2B' },
{ label: 'PDF/A-2U', value: '2U' },
{ label: 'PDF/A-3A', value: '3A' },
{ label: 'PDF/A-3B', value: '3B' },
{ label: 'PDF/A-3U', value: '3U' },
{ label: 'PDF/A-4', value: '4' },
{ label: 'PDF/A-4E', value: '4E' },
{ label: 'PDF/A-4F', value: '4F' },
];
// Validate PDF/A compliance
// This is the main function, which checks the PDF/A compliance of the currently loaded document
async function validatePdfa() {
let validResult = ""; // Replace with actual validation logic
const documentViewer = theInstance.Core.documentViewer;
const xfdfString = await theInstance.Core.annotationManager.exportAnnotations();
const currentDocument = documentViewer.getDocument();
const data = await currentDocument.getFileData({
xfdfString,
flags: theInstance.Core.SaveOptions.INCREMENTAL,
});
if(currentDocument.type !== 'pdf') {
labelStatus.textContent = '❌ Cannot validate PDF/A compliance. This document is not a PDF.';
labelStatus.style.backgroundColor = 'lightcoral';
return;
}
const pdfDoc = await currentDocument.getPDFDoc();
let pdfaVersion = await theInstance.Core.PDFNet.PDFACompliance.getDeclaredConformance(pdfDoc) - 1;
if(pdfaVersion === -1)
pdfaVersion = 0; // Default to PDF/A-1A if no conformance is declared
console.log(`PDF/A Version: ${pdfaVersion}`);
const conformanceLevel = theInstance.Core.PDFNet.PDFACompliance.Conformance[`e_Level${pdfaLevels[pdfaVersion].value}`];
const pdfa = await theInstance.Core.PDFNet.PDFACompliance.createFromBuffer(
false,
new Uint8Array(data),
'',
conformanceLevel
);
const errorCount = await pdfa.getErrorCount();
if (errorCount === 0) {
validResult = "✅ PDF/A Validation Successful. ";
validResult += `Standard: ${pdfaLevels[pdfaVersion].label}. `;
validResult += `Conformance Level: ${pdfaLevels[pdfaVersion].value}`;
labelStatus.style.backgroundColor = 'lightgreen';
} else {
labelStatus.style.backgroundColor = 'lightcoral';
validResult = "❌ PDF/A Validation Failed. This document is not a PDF/A.";
}
labelStatus.textContent = validResult;
}
const buttonValidate = document.createElement('button');
buttonValidate.textContent = 'Validate PDF/A';
buttonValidate.onclick = async () => {
validatePdfa();
};
const buttonUpload = document.createElement('button');
buttonUpload.textContent = 'Upload Local PDF';
buttonUpload.onclick = async () => {
fileUpload.click();
};
const labelStatus = document.createElement('label');
labelStatus.textContent = `Click "${buttonValidate.textContent}" to validate the PDF/A compliance of the document.`;
// UI section
//
// Helper code to add controls to the viewer holding the buttons and dropdown
// This code creates a container for the buttons and dropdown, styles them, and adds them to the viewer
// Create a container for all controls (file input buttons)
const controlsContainer = document.createElement('div');
const fileUpload = document.createElement('input');
fileUpload.style.display = 'none';
fileUpload.type = 'file';
fileUpload.accept = '.pdf';
fileUpload.onchange = (event) => {
const file = event.target.files[0];
if (file) {
labelStatus.style.backgroundColor = '';
labelStatus.textContent = `Loading document: ${file.name}`;
const reader = new FileReader();
reader.onload = () => {
theInstance.UI.loadDocument(reader.result, {filename: file.name});
};
reader.readAsArrayBuffer(file);
}
};
buttonUpload.className = 'btn-style';
buttonValidate.className = 'btn-style';
labelStatus.className = 'label-status';
controlsContainer.className = 'button-container';
controlsContainer.appendChild(fileUpload);
controlsContainer.appendChild(buttonUpload);
controlsContainer.appendChild(buttonValidate);
controlsContainer.appendChild(labelStatus);
element.insertBefore(controlsContainer, element.firstChild);