// ES6 Compliant Syntax
// GitHub Copilot - GPT-4 Model - August 26, 2025
// File: pdf-viewer-mobile/index.js
import WebViewer from '@pdftron/webviewer';
const licenseKey = 'YOUR_WEBVIEWER_LICENSE_KEY';
const devices = [
{ label: 'Galaxy S5', value: 'galaxys5', checked: true, size: { width: 360, height: 640 } },
{ label: 'Pixel 2', value: 'pixel2', size: { width: 411, height: 731 } },
{ label: 'iPhone 6/7/8', value: 'iphone678', size: { width: 375, height: 667 } },
{ label: 'iPhone X', value: 'iphonex', size: { width: 375, height: 812 } },
{ label: 'iPad', value: 'iPad', size: { width: 768, height: 1024 } },
{ label: 'iPad Pro', value: 'iPadPro', size: { width: 1024, height: 1366 } },
];
function setViewerSize() {
const width = parseInt(textWidth.value, 10);
const height = parseInt(textHeight.value, 10);
element.style.height = `${height}px`;
const inner = document.getElementById('wc-viewer');
inner.style.width = `${width}px`;
inner.style.height = `${height}px`;
}
const element = document.getElementById('viewer');
const onLoad = async () => {
element.style.display = 'flex';
element.style.alignItems = 'center';
element.style.justifyContent = 'center';
setTimeout(() => {
textWidth.value = devices[0].size.width;
textHeight.value = devices[0].size.height;
setViewerSize();
}, 500);
};
// Initialize WebViewer and load default document
WebViewer(
{
path: '/lib',
licenseKey: licenseKey,
initialDoc: 'https://apryse.s3.amazonaws.com/public/files/samples/magazine.pdf',
enableFilePicker: true, // Enable file picker to open files. In WebViewer -> menu icon -> Open File
},
element
).then(() => {
onLoad();
});
// UI section
//
// Helper code to add controls to the viewer holding the buttons and dropdown
// Create a container for all controls (labels, radio buttons and button)
const controlsContainer = document.createElement('div');
// Rotation state
let rotated = false;
// Create a radio group for devices
const deviceGroup = document.createElement('div');
deviceGroup.className = 'device-group';
devices.forEach(device => {
const label = document.createElement('label');
label.textContent = device.label;
label.htmlFor = device.value;
label.className = 'radio-label-class';
const radio = document.createElement('input');
radio.type = 'radio';
radio.name = 'device';
radio.id = device.value;
radio.value = device.value;
radio.checked = device.checked;
radio.onchange = () => {
const selectedDevice = devices.find(d => d.value === radio.value);
if (selectedDevice) {
let w = selectedDevice.size.width;
let h = selectedDevice.size.height;
if(rotated){
[w, h] = [h, w];
}
textWidth.value = w;
textHeight.value = h;
setViewerSize();
}
};
deviceGroup.appendChild(radio);
deviceGroup.appendChild(label);
});
controlsContainer.appendChild(deviceGroup);
const labelWidth = document.createElement('label');
labelWidth.textContent = 'W:';
labelWidth.className = 'label-class';
controlsContainer.appendChild(labelWidth);
const textWidth = document.createElement('input');
textWidth.type = 'text';
textWidth.className = 'text-style';
textWidth.oninput = () => {
setViewerSize();
};
controlsContainer.appendChild(textWidth);
const labelHeight = document.createElement('label');
labelHeight.textContent = 'H:';
labelHeight.className = 'label-class';
controlsContainer.appendChild(labelHeight);
const textHeight = document.createElement('input');
textHeight.type = 'text';
textHeight.className = 'text-style';
textHeight.oninput = () => {
setViewerSize();
};
controlsContainer.appendChild(textHeight);
// Create a button to swap dimensions
const buttonSwap = document.createElement('button');
buttonSwap.className = 'btn-style';
buttonSwap.onclick = async () => {
rotated = !rotated;
[textWidth.value, textHeight.value] = [textHeight.value, textWidth.value];
setViewerSize();
};
// button's image
const img = new Image(20, 20);
img.src = "images/mobile-rotate-blue6.svg";
img.style.width = "20px";
img.style.height = "20px";
buttonSwap.appendChild(img);
controlsContainer.appendChild(buttonSwap);
// Apply classes for styling using CSS
controlsContainer.className = 'control-container';
// Append elements to the controls container
element.parentElement.insertBefore(controlsContainer, element);