Skip to content

Commit be3ba98

Browse files
committed
1.0.9 - error handling, update docs, new tests, fix types export. Update app refs when document is opened form pdf.js
1 parent eaa8156 commit be3ba98

File tree

15 files changed

+594
-135
lines changed

15 files changed

+594
-135
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,7 @@ dist
176176

177177
# Vitepress
178178
docs/.vitepress/cache
179-
docs/.vitepress/dist
179+
docs/.vitepress/dist
180+
181+
# In any tests directory ignore __screenshots__ directory
182+
__screenshots__/

docs/.vitepress/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ export default defineConfig({
4747

4848
socialLinks: [
4949
{ icon: "github", link: "https://github.com/tuttarealstep/vue-pdf.js" },
50+
{
51+
icon: "npm",
52+
link: "https://www.npmjs.com/package/@tuttarealstep/vue-pdf.js",
53+
},
5054
],
5155
},
5256
});

docs/components/PdfViewer.vue

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ const options = reactive<NonNullable<VuePDFjsProps['options']>>({
2727
}
2828
})
2929
30+
const onErrorHandler = (error: Error) => {
31+
console.error('Error loading PDF:', error)
32+
alert('An error occurred with the PDF')
33+
}
34+
35+
const sourceOptions = {
36+
onError: onErrorHandler
37+
}
38+
3039
watchEffect(() => {
3140
if (options.toolbar) {
3241
options.toolbar.visible = !hideToolbar.value
@@ -62,6 +71,8 @@ const onPdfAppLoaded = () => {
6271
//Set the number of pages in the ref
6372
pages.value = e.pagesCount
6473
})
74+
75+
vuepdfjs.value.pdfApp.eventBus.on('documenterror', onErrorHandler)
6576
}
6677
6778
const pdf = 'https://raw.githubusercontent.com/mozilla/pdf.js/v4.10.38/web/compressed.tracemonkey-pldi-09.pdf'
@@ -73,8 +84,12 @@ const pdf = 'https://raw.githubusercontent.com/mozilla/pdf.js/v4.10.38/web/compr
7384
<div>
7485
<input type="checkbox" v-model="hideToolbar" /> Hide Toolbar
7586
<input type="checkbox" v-model="hideSidebar" /> Hide Sidebar
87+
<button type="button" class="custom-button">
88+
Load Invalid PDF
89+
</button>
7690
</div>
77-
<VuePDFjs ref="vuepdfjs" :source="pdf" :options="options" @pdf-app:loaded="onPdfAppLoaded" />
91+
<VuePDFjs ref="vuepdfjs" :source="pdf" :options="options" :source-options="sourceOptions"
92+
@pdf-app:loaded="onPdfAppLoaded" />
7893
</template>
7994

8095
<style>
@@ -85,4 +100,13 @@ const pdf = 'https://raw.githubusercontent.com/mozilla/pdf.js/v4.10.38/web/compr
85100
display: flex;
86101
flex-direction: column;
87102
}
103+
104+
.custom-button {
105+
padding: 0.25rem 0.75rem;
106+
background-color: #007bff;
107+
color: white;
108+
border: none;
109+
border-radius: 5px;
110+
cursor: pointer;
111+
}
88112
</style>

docs/examples/basic-examples.md

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,220 @@ body {
127127
}
128128
</style>
129129
```
130+
131+
## Handle errors
132+
133+
### Handle errors with the `usePDF` composable
134+
135+
In this example, we handle errors in the `usePDF` composable and display a message to the user.
136+
137+
```vue
138+
<script setup lang="ts">
139+
import { reactive, useTemplateRef } from "vue";
140+
import {
141+
VuePDFjs,
142+
usePDF,
143+
type VuePDFjsProps,
144+
} from "@tuttarealstep/vue-pdf.js";
145+
import "@tuttarealstep/vue-pdf.js/dist/style.css";
146+
import enUS_FTL from "@tuttarealstep/vue-pdf.js/l10n/en-US/viewer.ftl?raw";
147+
148+
const vuepdfjs = useTemplateRef<typeof VuePDFjs>("vuepdfjs");
149+
150+
const options = reactive<NonNullable<VuePDFjsProps["options"]>>({
151+
locale: {
152+
code: "en-US",
153+
ftl: enUS_FTL,
154+
},
155+
});
156+
157+
const {
158+
pdf: document,
159+
info,
160+
pages,
161+
} = usePDF("invalid-source", {
162+
onError: (error) => {
163+
console.error("Error loading the PDF file", error);
164+
alert("Error loading the PDF file");
165+
},
166+
});
167+
168+
console.log(document, info, pages);
169+
</script>
170+
171+
<template>
172+
<div id="app">
173+
<VuePDFjs ref="vuepdfjs" :source="document" :options="options" />
174+
</div>
175+
</template>
176+
177+
<style>
178+
html,
179+
body,
180+
#app {
181+
height: 100%;
182+
width: 100%;
183+
}
184+
185+
body {
186+
margin: 0;
187+
padding: 0;
188+
}
189+
</style>
190+
```
191+
192+
### Handle errors only with the `VuePDFjs` component
193+
194+
In this example, we handle errors only with the `VuePDFjs` component and display a message to the user.
195+
196+
```vue
197+
<script setup lang="ts">
198+
import { reactive, useTemplateRef } from "vue";
199+
import {
200+
VuePDFjs,
201+
usePDF,
202+
type VuePDFjsProps,
203+
} from "@tuttarealstep/vue-pdf.js";
204+
import "@tuttarealstep/vue-pdf.js/dist/style.css";
205+
import enUS_FTL from "@tuttarealstep/vue-pdf.js/l10n/en-US/viewer.ftl?raw";
206+
207+
const vuepdfjs = useTemplateRef<typeof VuePDFjs>("vuepdfjs");
208+
209+
const options = reactive<NonNullable<VuePDFjsProps["options"]>>({
210+
locale: {
211+
code: "en-US",
212+
ftl: enUS_FTL,
213+
},
214+
});
215+
216+
// Custom error handler
217+
const onPdfAppError = (error: unknown) => {
218+
console.error(error);
219+
220+
alert("An error occurred while loading the PDF document.");
221+
};
222+
223+
// Source options
224+
const sourceOptions = reactive<NonNullable<VuePDFjsProps["sourceOptions"]>>({
225+
onError: onPdfAppError,
226+
});
227+
228+
const source = "invalid-source";
229+
</script>
230+
231+
<template>
232+
<div id="app">
233+
<VuePDFjs
234+
ref="vuepdfjs"
235+
:source="source"
236+
:options="options"
237+
:source-options="sourceOptions"
238+
/>
239+
</div>
240+
</template>
241+
242+
<style>
243+
html,
244+
body,
245+
#app {
246+
height: 100%;
247+
width: 100%;
248+
}
249+
250+
body {
251+
margin: 0;
252+
padding: 0;
253+
}
254+
</style>
255+
```
256+
257+
### Handle pdf.js document errors
258+
259+
If we try to load a PDF document with the pdf.js interface it dispach a "documenterror" event. We can listen to this event and handle the error.
260+
261+
```vue
262+
<script setup lang="ts">
263+
import { reactive, useTemplateRef } from "vue";
264+
import {
265+
VuePDFjs,
266+
usePDF,
267+
type VuePDFjsProps,
268+
} from "@tuttarealstep/vue-pdf.js";
269+
import "@tuttarealstep/vue-pdf.js/dist/style.css";
270+
import enUS_FTL from "@tuttarealstep/vue-pdf.js/l10n/en-US/viewer.ftl?raw";
271+
272+
const vuepdfjs = useTemplateRef<typeof VuePDFjs>("vuepdfjs");
273+
274+
const options = reactive<NonNullable<VuePDFjsProps["options"]>>({
275+
locale: {
276+
code: "en-US",
277+
ftl: enUS_FTL,
278+
},
279+
});
280+
281+
// Custom error handler
282+
const onPdfAppError = (error: unknown) => {
283+
console.error(error);
284+
285+
alert("An error occurred with the PDF document.");
286+
};
287+
288+
const onPdfAppLoaded = () => {
289+
console.log("pdf-app:loaded");
290+
console.log(vuepdfjs.value?.pdfApp);
291+
292+
if (!vuepdfjs.value?.pdfApp) {
293+
return;
294+
}
295+
296+
// Wait for the pages to be loaded
297+
vuepdfjs.value.pdfApp.eventBus.on("pagesloaded", (e: any) => {
298+
// Search for a specific text in the document
299+
vuepdfjs.value?.pdfApp.eventBus.dispatch("find", {
300+
query: [
301+
"Dynamic languages such as JavaScript are more difficult to compile than statically typed ones.",
302+
],
303+
caseSensitive: false,
304+
entireWord: false,
305+
highlightAll: true,
306+
});
307+
});
308+
309+
// Listen to the documenterror event
310+
vuepdfjs.value.pdfApp.eventBus.on("documenterror", onPdfAppError);
311+
};
312+
313+
const {
314+
pdf: document,
315+
info,
316+
pages,
317+
} = usePDF("compressed.tracemonkey-pldi-09.pdf");
318+
319+
console.log(document, info, pages);
320+
</script>
321+
322+
<template>
323+
<div id="app">
324+
<VuePDFjs
325+
ref="vuepdfjs"
326+
:source="document"
327+
:options="options"
328+
@pdf-app:loaded="onPdfAppLoaded"
329+
/>
330+
</div>
331+
</template>
332+
333+
<style>
334+
html,
335+
body,
336+
#app {
337+
height: 100%;
338+
width: 100%;
339+
}
340+
341+
body {
342+
margin: 0;
343+
padding: 0;
344+
}
345+
</style>
346+
```

docs/index.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,22 @@ features:
3131

3232
<style>
3333
:root {
34-
--vp-home-hero-name-color: transparent;
35-
--vp-home-hero-name-background: -webkit-linear-gradient(45deg, #3fb984 45%, #31475e);
34+
--vp-home-hero-name-color: transparent !important;
35+
--vp-home-hero-name-background: -webkit-linear-gradient(45deg, #3fb984 45%, #31475e) !important;
3636

37-
--vp-home-hero-image-background-image: linear-gradient(45deg, #3fb984 50%, #31475e 50%);
38-
--vp-home-hero-image-filter: blur(44px);
37+
--vp-home-hero-image-background-image: linear-gradient(45deg, #3fb984 50%, #31475e 50%) !important;
38+
--vp-home-hero-image-filter: blur(44px) !important;
3939
}
4040

4141
@media (min-width: 640px) {
4242
:root {
43-
--vp-home-hero-image-filter: blur(56px);
43+
--vp-home-hero-image-filter: blur(56px) !important;
4444
}
4545
}
4646

4747
@media (min-width: 960px) {
4848
:root {
49-
--vp-home-hero-image-filter: blur(68px);
49+
--vp-home-hero-image-filter: blur(68px) !important;
5050
}
5151
}
5252
</style>

0 commit comments

Comments
 (0)