@@ -49,20 +49,20 @@ function pdfViewer({ content, pagination }) {
49
49
async init ( ) {
50
50
if ( pagination ) {
51
51
// Create zoom controls
52
- const controls = document . createElement ( 'div' ) ;
52
+ const controls = document . createElement ( 'div' )
53
53
controls . className =
54
- 'flex gap-2 p-2 justify-start sticky top-0 bg-white dark:bg-gray-800 z-10 border-b dark:border-gray-700 border-gray-200' ;
54
+ 'flex gap-2 p-2 justify-start sticky top-0 bg-white dark:bg-gray-800 z-10 border-b dark:border-gray-700 border-gray-200'
55
55
controls . innerHTML = `
56
56
<button class="px-3 py-1 rounded bg-gray-100 hover:bg-gray-200 dark:bg-gray-700 dark:hover:bg-gray-600" @click="onPrevPage()">Previous</button>
57
57
<button class="px-3 py-1 rounded bg-gray-100 hover:bg-gray-200 dark:bg-gray-700 dark:hover:bg-gray-600" @click="onNextPage()">Next</button>
58
58
<span class="px-3 py-1">Page: <span id="page_num"></span> / <span id="page_count"></span></span>
59
- ` ;
60
- this . parent . appendChild ( controls ) ;
59
+ `
60
+ this . parent . appendChild ( controls )
61
61
62
62
// Create a container for the PDF content
63
- const pageContainer = document . createElement ( 'div' ) ;
64
- pageContainer . id = 'pdf-content' ;
65
- this . parent . appendChild ( pageContainer ) ;
63
+ const pageContainer = document . createElement ( 'div' )
64
+ pageContainer . id = 'pdf-content'
65
+ this . parent . appendChild ( pageContainer )
66
66
67
67
await this . renderPage ( )
68
68
} else {
@@ -72,18 +72,15 @@ function pdfViewer({ content, pagination }) {
72
72
this . parent . innerHTML = ''
73
73
74
74
// Add timestamp to force refresh
75
- const refreshedUrl = this . baseUrl + '?t=' + new Date ( ) . getTime ( )
75
+ const refreshedUrl =
76
+ this . baseUrl + '?t=' + new Date ( ) . getTime ( )
76
77
await this . render ( refreshedUrl )
77
78
} )
78
79
}
79
80
} ,
80
81
81
82
// Function to calculate optimal scale
82
- calculateOptimalScale (
83
- page ,
84
- containerWidth ,
85
- containerHeight ,
86
- ) {
83
+ calculateOptimalScale ( page , containerWidth , containerHeight ) {
87
84
const viewport = page . getViewport ( { scale : 1.0 } )
88
85
const containerAspectRatio = containerWidth / containerHeight
89
86
const pageAspectRatio = viewport . width / viewport . height
@@ -99,93 +96,99 @@ function pdfViewer({ content, pagination }) {
99
96
} ,
100
97
101
98
async renderPage ( ) {
102
- const pageContainer = this . parent . querySelector ( '#pdf-content' ) ;
103
- pageContainer . innerHTML = '' ; // Clear only the PDF content
99
+ const pageContainer = this . parent . querySelector ( '#pdf-content' )
100
+ pageContainer . innerHTML = '' // Clear only the PDF content
104
101
105
- this . parent . style . cssText = 'width: 100%; overflow: auto; position: relative;' ;
102
+ this . parent . style . cssText =
103
+ 'width: 100%; overflow: auto; position: relative;'
106
104
this . pageRendering = true
107
105
108
106
const loadingTask = pdfjsLib . getDocument ( this . baseUrl )
109
- loadingTask . promise . then ( async ( pdf ) => {
110
- const containerWidth = this . parent . clientWidth
111
- const containerHeight = this . parent . clientHeight
112
- this . totalPages = pdf . numPages
113
- document . getElementById ( 'page_count' ) . textContent = this . totalPages
114
- document . getElementById ( 'page_num' ) . textContent = this . pageNumber ;
115
-
116
- // Render page
117
- pdf . getPage ( this . pageNumber ) . then ( async ( page ) => {
118
- const scale = this . calculateOptimalScale (
119
- page ,
120
- containerWidth ,
121
- containerHeight ,
122
- )
123
- const viewport = page . getViewport ( { scale } )
124
-
125
- // Prepare canvas using PDF page dimensions
126
- const canvas = document . createElement ( 'canvas' )
127
- const context = canvas . getContext ( '2d' )
128
-
129
- canvas . width = viewport . width
130
- canvas . height = viewport . height
131
-
132
- // Canvas styling
133
- canvas . style . cssText = 'display: block; margin: 10px auto;'
134
-
135
- // Render PDF page into canvas context
136
- const renderContext = {
137
- canvasContext : context ,
138
- viewport : viewport ,
139
- }
140
- await page . render ( renderContext ) . promise . then ( ( ) => {
141
- this . pageRendering = false
142
- } )
143
-
144
- // Render text layer
145
- const textContent = await page . getTextContent ( )
146
- const textLayerDiv = document . createElement ( 'div' )
147
- textLayerDiv . className = 'textLayer'
148
- textLayerDiv . style . cssText = 'margin-left: 15px;'
149
- const textLayer = new pdfjsLib . TextLayer ( {
150
- textContentSource : textContent ,
151
- container : textLayerDiv ,
152
- viewport : viewport ,
153
- } )
154
- await textLayer . render ( ) . then ( ( ) => {
155
- const pageDiv = document . createElement ( 'div' )
156
- pageDiv . className = 'page'
157
- pageDiv . style . cssText = 'position: relative;'
158
- pageDiv . appendChild ( canvas )
159
- pageDiv . appendChild ( textLayerDiv )
160
- pageContainer . appendChild ( pageDiv )
107
+ loadingTask . promise
108
+ . then ( async ( pdf ) => {
109
+ const containerWidth = this . parent . clientWidth
110
+ const containerHeight = this . parent . clientHeight
111
+ this . totalPages = pdf . numPages
112
+ document . getElementById ( 'page_count' ) . textContent =
113
+ this . totalPages
114
+ document . getElementById ( 'page_num' ) . textContent =
115
+ this . pageNumber
116
+
117
+ // Render page
118
+ pdf . getPage ( this . pageNumber ) . then ( async ( page ) => {
119
+ const scale = this . calculateOptimalScale (
120
+ page ,
121
+ containerWidth ,
122
+ containerHeight ,
123
+ )
124
+ const viewport = page . getViewport ( { scale } )
125
+
126
+ // Prepare canvas using PDF page dimensions
127
+ const canvas = document . createElement ( 'canvas' )
128
+ const context = canvas . getContext ( '2d' )
129
+
130
+ canvas . width = viewport . width
131
+ canvas . height = viewport . height
132
+
133
+ // Canvas styling
134
+ canvas . style . cssText =
135
+ 'display: block; margin: 10px auto;'
136
+
137
+ // Render PDF page into canvas context
138
+ const renderContext = {
139
+ canvasContext : context ,
140
+ viewport : viewport ,
141
+ }
142
+ await page . render ( renderContext ) . promise . then ( ( ) => {
143
+ this . pageRendering = false
144
+ } )
145
+
146
+ // Render text layer
147
+ const textContent = await page . getTextContent ( )
148
+ const textLayerDiv = document . createElement ( 'div' )
149
+ textLayerDiv . className = 'textLayer'
150
+ textLayerDiv . style . cssText = 'margin-left: 15px;'
151
+ const textLayer = new pdfjsLib . TextLayer ( {
152
+ textContentSource : textContent ,
153
+ container : textLayerDiv ,
154
+ viewport : viewport ,
155
+ } )
156
+ await textLayer . render ( ) . then ( ( ) => {
157
+ const pageDiv = document . createElement ( 'div' )
158
+ pageDiv . className = 'page'
159
+ pageDiv . style . cssText = 'position: relative;'
160
+ pageDiv . appendChild ( canvas )
161
+ pageDiv . appendChild ( textLayerDiv )
162
+ pageContainer . appendChild ( pageDiv )
163
+ } )
161
164
} )
162
165
} )
163
- } ) . catch ( function ( error ) {
164
- console . error ( 'Error loading PDF:' , error )
165
- pageContainer . innerHTML = `
166
+ . catch ( function ( error ) {
167
+ console . error ( 'Error loading PDF:' , error )
168
+ pageContainer . innerHTML = `
166
169
<div class="p-4">
167
170
<p class="text-red-500">Error loading PDF:</p>
168
171
<p class="text-sm mt-2">${ error . message } </p>
169
172
</div>
170
173
`
171
- } )
174
+ } )
172
175
} ,
173
176
174
177
onPrevPage ( ) {
175
178
if ( this . pageNumber <= 1 ) {
176
- return ;
179
+ return
177
180
}
178
- this . pageNumber -- ;
179
- this . renderPage ( ) ;
181
+ this . pageNumber --
182
+ this . renderPage ( )
180
183
} ,
181
184
182
185
onNextPage ( ) {
183
186
if ( this . pageNumber >= this . totalPages ) {
184
- return ;
187
+ return
185
188
}
186
- this . pageNumber ++ ;
187
- this . renderPage ( ) ;
188
- }
189
+ this . pageNumber ++
190
+ this . renderPage ( )
191
+ } ,
189
192
}
190
193
}
191
194
0 commit comments