0

Trying to pass in a pdf to a svelte script, to convert the pages to pdf.

<script>
import { onMount } from 'svelte';
import { convertPdfToPng } from 'convert-pdf-png';

let files;


const convertAndDoSomething=(files)=>
{
    const pdf = files[0];
    console.log("been called");

    convertPdfToPng(pdf, {
        outputType: 'callback',
        callback: callback
    });

    const callback = images => {
        // the function returns an array
        // every img is a normal file object
        images.forEach(img => {
            console.log(img.filename);
        });
    }
};


</script>


<input type="file" bind:files>

{#if files && files[0]}
    <p use:convertAndDoSomething>
        {files[0].name}
    </p>
{/if}

this gives an error: Uncaught (in promise) ReferenceError: Cannot access 'callback' before initialization at convertAndDoSomething (Example6.svelte:17:19) at Object.mount [as m] (Example6.svelte:38:6) at Object.update [as p] (Example6.svelte:35:22) at update (index.mjs:1075:36) at flush (index.mjs:1042:13)

dram
  • 467
  • 2
  • 5
  • 11
  • As the error says... You cannot use `callback` in `convertPdfToPng(pdf, { ... callback: callback })` when `callback` is initialized after that call -> [Are variables declared with let or const hoisted?](https://stackoverflow.com/questions/31219420/are-variables-declared-with-let-or-const-hoisted) – Andreas Jan 28 '22 at 09:44
  • it is only "pdf" that is declared here as const – dram Jan 28 '22 at 10:27
  • No o.O -> `const callback = images => { ... }` – Andreas Jan 28 '22 at 11:08

0 Answers0