Simple svelte PDF Viewer component with controls like
npm install @defense-unicorns/svelte-pdf-viewer
<script>
import PdfViewer from '@defense-unicorns/svelte-pdf-viewer';
</script>
<PdfViewer url='./sample.pdf' />
<script>
import PdfViewer from '@defense-unicorns/svelte-pdf-viewer';
</script>
<PdfViewer url='https://raw.githubusercontent.com/vinodnimbalkar/svelte-pdf-viewer/369db2f9edbf5ab8c87184193e1404340729bb3a/public/sample.pdf' />
base64
encoded string<script>
import PdfViewer from 'svelte-pdf-viewer';
const base64 =
"JVBERi0xLjcKCjEgMCBvYmogICUgZW50cnkgcG9pbnQKPDwKICAvVHlwZSAvQ2F0YWxvZwogIC9QYWdlcyAyIDAgUgo+PgplbmRvYmoKCjIgMCBvYmoKPDwKICAvVHlwZSAvUGFnZXMKICAvTWVkaWFCb3ggWyAwIDAgMjAwIDIwMCBdCiAgL0NvdW50IDEKICAvS2lkcyBbIDMgMCBSIF0KPj4KZW5kb2JqCgozIDAgb2JqCjw8CiAgL1R5cGUgL1BhZ2UKICAvUGFyZW50IDIgMCBSCiAgL1Jlc291cmNlcyA8PAogICAgL0ZvbnQgPDwKICAgICAgL0YxIDQgMCBSIAogICAgPj4KICA+PgogIC9Db250ZW50cyA1IDAgUgo+PgplbmRvYmoKCjQgMCBvYmoKPDwKICAvVHlwZSAvRm9udAogIC9TdWJ0eXBlIC9UeXBlMQogIC9CYXNlRm9udCAvVGltZXMtUm9tYW4KPj4KZW5kb2JqCgo1IDAgb2JqICAlIHBhZ2UgY29udGVudAo8PAogIC9MZW5ndGggNDQKPj4Kc3RyZWFtCkJUCjcwIDUwIFRECi9GMSAxMiBUZgooSGVsbG8sIHdvcmxkISkgVGoKRVQKZW5kc3RyZWFtCmVuZG9iagoKeHJlZgowIDYKMDAwMDAwMDAwMCA2NTUzNSBmIAowMDAwMDAwMDEwIDAwMDAwIG4gCjAwMDAwMDAwNzkgMDAwMDAgbiAKMDAwMDAwMDE3MyAwMDAwMCBuIAowMDAwMDAwMzAxIDAwMDAwIG4gCjAwMDAwMDAzODAgMDAwMDAgbiAKdHJhaWxlcgo8PAogIC9TaXplIDYKICAvUm9vdCAxIDAgUgo+PgpzdGFydHhyZWYKNDkyCiUlRU9G";
</script>
<PdfViewer data={atob(base64)} />
Prop | Type | Default | Req |
---|---|---|---|
url |
string |
N/A |
Yes |
data |
string |
N/A |
No |
mode |
string |
"dark" |
No |
scale |
float |
1.5 |
No |
pageNum |
number |
1 |
No |
flipTime |
number |
120 |
No |
showButtons |
array |
"['navigation', 'zoom', 'print', 'rotate', 'download', 'autoflip', 'timeInfo', 'pageInfo']" |
No |
showBorder |
boolean |
true |
No |
downloadFileName |
string |
N/A |
No |
To view the examples, clone the @defense-unicorns/svelte-pdf-viewer repo and install the dependencies:
git clone [email protected]:defenseunicorns/svelte-pdf-viewer.git
cd example
npm install
npm run dev
Then run the http://localhost:5000:
devDependencies
When using Svelte components installed from npm, it needs the original component source (rather than any precompiled JavaScript that ships with the component). This allows the component to be rendered server-side, and also keeps your client-side app smaller...
-- [Rich Harris](https://github.com/Rich-Harris/svelte-workshop#using-external-components)
We have to install @defense-unicorns/svelte-pdf-viewer
as part of devDependencies
npm install -D @defense-unicorns/svelte-pdf-viewer
...this will cause it to get bundled (and therefore compiled) with your app.
PdfViewer
component SSR compatibleSince out PdfViewer
component has a dependency on window
object, we have to use dynamic import, from within the
onMount
function (which is only called on the client), so that our import code is never called on the server.
Refer to the official doc here...
<script>
import { onMount } from "svelte";
let PdfViewer;
onMount(async () => {
const module = await import("@defense-unicorns/svelte-pdf-viewer");
PdfViewer = module.default;
});
</script>
<svelte:component this={PdfViewer} url="YOUR-PDF-URL"/>
Feel free to open an issue (or even better, send a Pull Request). Contributions are very welcome!! 😄