1// ES6 Compliant Syntax
2// GitHub Copilot v1.0 - Model: Claude-3.5 Sonnet - July 9, 2025
3// File: examples/pdfa-conversion/index.js
4import WebViewer from '@pdftron/webviewer';
5
6const licenseKey = 'YOUR_WEBVIEWER_LICENSE_KEY';
7
8const element = document.getElementById('viewer');
9let instCore = null;
10const onLoad = async (instance) => {
11 instCore = instance.Core;
12};
13
14WebViewer(
15 {
16 path: '/lib',
17 licenseKey: licenseKey,
18 initialDoc: 'https://apryse.s3.amazonaws.com/public/files/samples/WebviewerDemoDoc.pdf',
19 fullAPI: true,
20 },
21 element
22).then((instance) => {
23 onLoad(instance);
24});
25
26let pdfaBlob = null;
27
28const buttonConvert = document.createElement('button');
29buttonConvert.textContent = 'PDF/A convert';
30buttonConvert.onclick = async () => {
31 const doc = instCore.documentViewer.getDocument();
32 const xfdfString = await instCore.annotationManager.exportAnnotations();
33 const data = await doc.getFileData({
34 xfdfString,
35 });
36
37 const conformanceLevel = instCore.PDFNet.PDFACompliance.Conformance[`${levelSelect.value}`];
38 const PDFNetFile = await instCore.PDFNet.PDFACompliance.createFromBuffer(
39 true,
40 data,
41 '',
42 conformanceLevel
43 );
44 const buf = await PDFNetFile.saveAsFromBuffer(false);
45 pdfaBlob = new Blob([buf], { type: 'application/pdf' });
46
47 console.log("PDF/A conversion successful.");
48};
49
50// UI section
51//
52// Helper code to add controls to the viewer holding the buttons and dropdown
53// This code creates a container for the buttons and dropdown, styles them, and adds them to the viewer
54//
55
56
57const buttonDownload = document.createElement('button');
58buttonDownload.textContent = 'Download PDF/A';
59buttonDownload.onclick = () => {
60 if (pdfaBlob) {
61 const url = URL.createObjectURL(pdfaBlob);
62 const link = document.createElement('a');
63 link.href = url;
64 link.download = 'converted.pdf';
65 link.click();
66 } else {
67 alert('PDF/A not available to download.\nPlease convert the document to PDF/A first.');
68 }
69};
70
71// Create a dropdown for PDF/A levels
72// This dropdown allows users to select the PDF/A compliance level they want to convert to.
73// The options correspond to the different PDF/A levels supported by PDFNet.
74
75const levelSelect = document.createElement('select');
76const levels = [
77 { value: 'e_Level1A', label: 'PDF/A-1a' },
78 { value: 'e_Level1B', label: 'PDF/A-1b' },
79 { value: 'e_Level2A', label: 'PDF/A-2a' },
80 { value: 'e_Level2B', label: 'PDF/A-2b' },
81 { value: 'e_Level2U', label: 'PDF/A-2u' },
82 { value: 'e_Level3A', label: 'PDF/A-3a' },
83 { value: 'e_Level3B', label: 'PDF/A-3b' },
84 { value: 'e_Level3U', label: 'PDF/A-3u' },
85 { value: 'e_Level4', label: 'PDF/A-4' },
86 { value: 'e_Level4E', label: 'PDF/A-4e' },
87 { value: 'e_Level4F', label: 'PDF/A-4f' },
88];
89
90// Populate the dropdown with options
91levels.forEach(level => {
92 const option = document.createElement('option');
93 option.value = level.value;
94 option.textContent = level.label;
95 levelSelect.appendChild(option);
96});
97
98// set default value for the dropdown
99levelSelect.value = levels[3].value; // Default to PDF/A-2b
100levelSelect.oninput = () => {
101 console.log(`Selected PDF/A level: ${levelSelect.value}`);
102 pdfaBlob = null; // Reset the blob when the level changes
103};
104
105const levelLabel = document.createElement('label');
106levelLabel.textContent = 'Select PDF/A level: ';
107
108// Create a container for all controls (label, dropdown, and buttons)
109const controlsContainer = document.createElement('div');
110
111levelLabel.className = 'level-label';
112buttonDownload.className = 'btn-download';
113buttonConvert.className = 'btn-convert';
114levelSelect.className = 'level-select';
115controlsContainer.className = 'button-container';
116
117controlsContainer.appendChild(levelLabel);
118controlsContainer.appendChild(levelSelect);
119controlsContainer.appendChild(buttonConvert);
120controlsContainer.appendChild(buttonDownload);
121element.insertBefore(controlsContainer, element.firstChild);