@@ -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+ ```
0 commit comments