See a side-by-side comparison of a PDF viewer rendering an unoptimized PDF vs an optimized PDF using this JavaScript sample. Optimized PDFs render in viewers faster, giving users access to even large and complex PDF documents in seconds. This sample works on all browsers (including IE11) and mobile devices without using plug-ins. Learn more about our Web SDK.
1const FILES = {
2 'demo-annotated.pdf': ['../../../samples/files/demo-annotated-non-optimized.pdf', '../../../samples/files/demo-annotated.pdf'],
3 'construction-drawing-final.pdf': ['../../../samples/files/construction-drawing-final.pdf', '../../../samples/files/construction-drawing-optimized.pdf'],
4 'cheetahs.pdf': ['../../../samples/files/cheetahs-non-optimized.pdf', '../../../samples/files/cheetahs.pdf'],
5 'magazine.pdf': ['https://pdftron.s3.amazonaws.com/downloads/pl/magazine.pdf', 'https://pdftron.s3.amazonaws.com/downloads/pl/magazine-optimized.pdf'],
6 'webviewer-demo.pdf': ['https://pdftron.s3.amazonaws.com/downloads/pl/webviewer-demo.pdf', 'https://pdftron.s3.amazonaws.com/downloads/pl/webviewer-demo-optimized.pdf'],
7};
8
9const _selected = localStorage.getItem('__selectedFiles');
10const _selectedFiles = FILES[_selected];
11
12const firstPdf = _selectedFiles ? _selectedFiles[0] : null;
13const secondPdf = _selectedFiles ? _selectedFiles[1] : null;
14const firstPdfUrl = localStorage.getItem('__firstPdfUrl');
15const secondPdfUrl = localStorage.getItem('__secondPdfUrl');
16const isFullAPI = localStorage.getItem('__isFullAPI') || false;
17
18let sourceOne;
19let sourceTwo;
20
21if (firstPdfUrl && secondPdfUrl) {
22 document.getElementById('url').value = firstPdfUrl;
23 document.getElementById('url2').value = secondPdfUrl;
24 sourceOne = firstPdfUrl;
25 sourceTwo = secondPdfUrl;
26
27 document.getElementById('select1').value = undefined;
28} else {
29 const defaultSelected = 'cheetahs.pdf';
30 const _selectedFiles = FILES[defaultSelected];
31 sourceOne = firstPdf || _selectedFiles[0];
32 sourceTwo = secondPdf || _selectedFiles[1];
33
34 document.getElementById('select1').value = _selected || defaultSelected;
35}
36document.getElementById('isFullAPI').checked = isFullAPI;
37
38const timerOne = Date.now();
39const timerTwo = Date.now();
40
41// ============== WebViewer setup =============
42WebViewer(
43 {
44 path: '../../../lib',
45 initialDoc: sourceOne,
46 fullAPI: isFullAPI,
47 },
48 document.getElementById('middlePanel')
49).then(instance => {
50 instance.Core.documentViewer.addEventListener('documentLoaded', () => {
51 const timerOneFinish = Date.now();
52 console.log('The first viewer', timerOneFinish - timerOne);
53 });
54});
55
56WebViewer(
57 {
58 path: '../../../lib',
59 initialDoc: sourceTwo,
60 fullAPI: isFullAPI,
61 },
62 document.getElementById('leftPanel')
63).then(instance => {
64 instance.Core.documentViewer.addEventListener('documentLoaded', () => {
65 const timerTwoFinish = Date.now();
66 console.log('The second viewer', timerTwoFinish - timerTwo);
67 });
68});
69
70// ============== End WebViewer setup =============
71
72// ============== Setup input controllers =============
73document.getElementById('selectControl').onclick = e => {
74 e.preventDefault();
75 const select1 = document.getElementById('select1');
76 const selected = select1.options[select1.selectedIndex].value;
77
78 localStorage.setItem('__selectedFiles', selected);
79 localStorage.removeItem('__firstPdfUrl');
80 localStorage.removeItem('__secondPdfUrl');
81 location.reload();
82};
83
84document.getElementById('url-form').onsubmit = e => {
85 e.preventDefault();
86 const firstPdf = document.getElementById('url').value;
87 const secondPdf = document.getElementById('url2').value;
88
89 localStorage.setItem('__firstPdfUrl', firstPdf);
90 localStorage.setItem('__secondPdfUrl', secondPdf);
91
92 localStorage.removeItem('__selectedFiles');
93 location.reload();
94};
95
96document.getElementById('isFullAPI').onchange = e => {
97 if (e.target.checked) {
98 localStorage.setItem('__isFullAPI', true);
99 } else {
100 localStorage.removeItem('__isFullAPI');
101 }
102};
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales