File size: 1,900 Bytes
1b10823
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3628e7
1b10823
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3628e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<script lang="ts">
    import pdfjsLib from "pdfjs-dist";
    pdfjsLib.GlobalWorkerOptions.workerSrc = "https://cdn.jsdelivr.net/npm/pdfjs-dist/build/pdf.worker.min.js";

    let pdfDoc;
    let numPages = 1;
    let currentPage = 1;
    let canvasRef;
    export let value;

    async function loadPDF(value) {
        const loadingTask = pdfjsLib.getDocument(value.url);
        pdfDoc = await loadingTask.promise;
        numPages = pdfDoc.numPages;
        currentPage = 1;
        renderPage();
    }

    function renderPage() {
        pdfDoc.getPage(currentPage).then((page) => {
            const viewport = page.getViewport({ scale: 1.5 });
            const canvasContext = canvasRef.getContext("2d");
            canvasRef.width = viewport.width;
            canvasRef.height = viewport.height;

            page.render({
                canvasContext,
                viewport,
            });
        });
    }

    function nextPage() {
        if (currentPage < numPages) {
            currentPage++;
            renderPage();
        }
    }

    function prevPage() {
        if (currentPage > 1) {
            currentPage--;
            renderPage();
        }
    }

    $: if (value?.url) {
        loadPDF(value);
    }
</script>

<div>
    <div class="pdf-viewer">
        <canvas bind:this={canvasRef}></canvas>
    </div>
    <div class="controls">
        <button on:click={prevPage} disabled={currentPage === 1}>⬅️ Anterior</button>
        <span>{currentPage} / {numPages}</span>
        <button on:click={nextPage} disabled={currentPage === numPages}>Siguiente ➡️</button>
    </div>
</div>

<style>
    .pdf-viewer {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 500px;
    }
    .controls {
        display: flex;
        justify-content: center;
        gap: 10px;
        margin-top: 10px;
    }
</style>