1
- const $ pdf = 'document.pdf' ;
1
+ const pdf = 'document.pdf' ;
2
2
3
- const $initialState = {
3
+ // Import the necessary module from pdf.js
4
+ import { GlobalWorkerOptions , getDocument } from 'https://cdn.jsdelivr.net/npm/[email protected] /build/pdf.min.mjs' ;
5
+
6
+ // Set worker source
7
+ GlobalWorkerOptions . workerSrc = 'https://cdn.jsdelivr.net/npm/[email protected] /build/pdf.worker.min.mjs' ;
8
+
9
+ const initialState = {
4
10
pdfDoc : null ,
5
11
currentPage : 1 ,
6
12
pageCount : 0 ,
@@ -9,97 +15,86 @@ const $initialState = {
9
15
10
16
// Render the page
11
17
const renderPage = ( ) => {
12
- // load the first page
13
- $initialState . pdfDoc . getPage ( $initialState . currentPage ) . then ( ( page ) => {
14
- console . log ( 'page' , page ) ;
15
-
16
- const canvas = $ ( '#canvas' ) [ 0 ] ;
17
- const $ctx = canvas . getContext ( '2d' ) ;
18
- const $viewport = page . getViewport ( { scale : $initialState . zoom } ) ;
18
+ initialState . pdfDoc . getPage ( initialState . currentPage ) . then ( ( page ) => {
19
+ const canvas = $ ( "#canvas" ) [ 0 ] ;
20
+ const ctx = canvas . getContext ( "2d" ) ;
21
+ const viewport = page . getViewport ( { scale : initialState . zoom } ) ;
19
22
20
- canvas . height = $ viewport. height ;
21
- canvas . width = $ viewport. width ;
23
+ canvas . height = viewport . height ;
24
+ canvas . width = viewport . width ;
22
25
23
26
// Render PDF page into canvas context
24
27
const renderCtx = {
25
- canvasContext : $ ctx,
26
- viewport : $ viewport,
28
+ canvasContext : ctx ,
29
+ viewport : viewport ,
27
30
} ;
28
31
29
32
page . render ( renderCtx ) ;
30
33
31
- $ ( '#page_num' ) . html ( $initialState . currentPage ) ;
34
+ // Update current page number
35
+ $ ( "#page_num" ) . text ( initialState . currentPage ) ;
32
36
} ) ;
33
37
} ;
34
38
35
39
// Load the Document
36
- pdfjsLib
37
- . getDocument ( $pdf )
38
- . promise . then ( ( doc ) => {
39
- $initialState . pdfDoc = doc ;
40
- console . log ( 'pdfDocument' , $initialState . pdfDoc ) ;
40
+ getDocument ( pdf ) . promise . then ( ( data ) => {
41
+ initialState . pdfDoc = data ;
42
+ $ ( "#page_count" ) . text ( initialState . pdfDoc . numPages ) ;
41
43
42
- $ ( '#page_count' ) . html ( $initialState . pdfDoc . numPages ) ;
44
+ renderPage ( ) ;
45
+ } ) . catch ( ( err ) => {
46
+ alert ( err . message ) ;
47
+ } ) ;
43
48
44
- renderPage ( ) ;
45
- } )
46
- . catch ( ( err ) => {
47
- alert ( err . message ) ;
48
- } ) ;
49
+ // Show Previous Page
50
+ const showPrevPage = ( ) => {
51
+ if ( initialState . pdfDoc === null || initialState . currentPage <= 1 ) return ;
52
+ initialState . currentPage -- ;
53
+ $ ( "#current_page" ) . val ( initialState . currentPage ) ;
54
+ renderPage ( ) ;
55
+ } ;
49
56
50
- function showPrevPage ( ) {
51
- if ( $initialState . pdfDoc === null || $initialState . currentPage <= 1 ) return ;
52
- $ initialState. currentPage -- ;
53
- // render the current page
54
- $ ( ' #current_page' ) . val ( $ initialState. currentPage ) ;
57
+ // Show Next Page
58
+ const showNextPage = ( ) => {
59
+ if ( initialState . pdfDoc === null || initialState . currentPage >= initialState . pdfDoc . numPages ) return ;
60
+ initialState . currentPage ++ ;
61
+ $ ( " #current_page" ) . val ( initialState . currentPage ) ;
55
62
renderPage ( ) ;
56
- }
63
+ } ;
57
64
58
- function showNextPage ( ) {
59
- if (
60
- $ initialState. pdfDoc === null ||
61
- $ initialState. currentPage >= $initialState . pdfDoc . _pdfInfo . numPages
62
- )
63
- return ;
65
+ // Zoom In
66
+ const zoomIn = ( ) => {
67
+ if ( initialState . pdfDoc === null ) return ;
68
+ initialState . zoom *= 1.25 ;
69
+ renderPage ( ) ;
70
+ } ;
64
71
65
- $initialState . currentPage ++ ;
66
- $ ( '#current_page' ) . val ( $initialState . currentPage ) ;
72
+ // Zoom Out
73
+ const zoomOut = ( ) => {
74
+ if ( initialState . pdfDoc === null ) return ;
75
+ initialState . zoom *= 0.8 ;
67
76
renderPage ( ) ;
68
- }
69
-
70
- // Button Events
71
- $ ( '#prev-page' ) . click ( showPrevPage ) ;
72
- $ ( '#next-page' ) . click ( showNextPage ) ;
73
-
74
- // Display a specific page
75
- $ ( '#current_page' ) . on ( 'keypress' , ( event ) => {
76
- if ( $initialState . pdfDoc === null ) return ;
77
- // get the key code
78
- const $keycode = event . keyCode ? event . keyCode : event . which ;
79
- if ( $keycode === 13 ) {
80
- // get the new page number and render it
81
- let desiredPage = $ ( '#current_page' ) [ 0 ] . valueAsNumber ;
82
-
83
- $initialState . currentPage = Math . min (
84
- Math . max ( desiredPage , 1 ) ,
85
- $initialState . pdfDoc . _pdfInfo . numPages
86
- ) ;
87
- renderPage ( ) ;
77
+ } ;
88
78
89
- $ ( '#current_page' ) . val ( $initialState . currentPage ) ;
90
- }
91
- } ) ;
79
+ // Bind events using jQuery
80
+ $ ( "#prev_page" ) . on ( "click" , showPrevPage ) ;
81
+ $ ( "#next_page" ) . on ( "click" , showNextPage ) ;
82
+ $ ( "#zoom_in" ) . on ( "click" , zoomIn ) ;
83
+ $ ( "#zoom_out" ) . on ( "click" , zoomOut ) ;
92
84
93
- // Zoom functionality
94
- $ ( '#zoom_in' ) . on ( 'click' , ( ) => {
95
- if ( $initialState . pdfDoc === null ) return ;
96
- $initialState . zoom *= 4 / 3 ;
85
+ // Keypress Event for jumping to a page
86
+ $ ( "#current_page" ) . on ( "keypress" , ( event ) => {
87
+ if ( initialState . pdfDoc === null ) return ;
97
88
98
- renderPage ( ) ;
89
+ if ( event . keyCode === 13 ) {
90
+ let desiredPage = parseInt ( $ ( "#current_page" ) . val ( ) ) ;
91
+ initialState . currentPage = Math . min ( Math . max ( desiredPage , 1 ) , initialState . pdfDoc . numPages ) ;
92
+ $ ( "#current_page" ) . val ( initialState . currentPage ) ;
93
+ renderPage ( ) ;
94
+ }
99
95
} ) ;
100
96
101
- $ ( '#zoom_out' ) . on ( 'click' , ( ) => {
102
- if ( $initialState . pdfDoc === null ) return ;
103
- $initialState . zoom *= 2 / 3 ;
104
- renderPage ( ) ;
97
+ // Optional Print Support
98
+ $ ( ".print-button" ) . on ( "click" , ( ) => {
99
+ window . print ( ) ;
105
100
} ) ;
0 commit comments