Skip to content

Commit

Permalink
Merge pull request #38 from vinodnimbalkar/new-feature
Browse files Browse the repository at this point in the history
feat: base64 string encode support added
  • Loading branch information
vinodnimbalkar authored Aug 21, 2022
2 parents 0652d68 + 452c229 commit 7962eed
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,42 @@ npm install svelte-pdf
```

## How to use
#### Using local path
```js
<script>
import PdfViewer from 'svelte-pdf';
</script>

<PdfViewer url='./sample.pdf' />

```
#### Using url
```js
<script>
import PdfViewer from 'svelte-pdf';
</script>

<PdfViewer url='https://raw.githubusercontent.com/vinodnimbalkar/svelte-pdf/369db2f9edbf5ab8c87184193e1404340729bb3a/public/sample.pdf' />

```
#### Using `base64` encoded string
```js
<script>
import PdfViewer from 'svelte-pdf';
const base64 =
"JVBERi0xLjcKCjEgMCBvYmogICUgZW50cnkgcG9pbnQKPDwKICAvVHlwZSAvQ2F0YWxvZwogIC9QYWdlcyAyIDAgUgo+PgplbmRvYmoKCjIgMCBvYmoKPDwKICAvVHlwZSAvUGFnZXMKICAvTWVkaWFCb3ggWyAwIDAgMjAwIDIwMCBdCiAgL0NvdW50IDEKICAvS2lkcyBbIDMgMCBSIF0KPj4KZW5kb2JqCgozIDAgb2JqCjw8CiAgL1R5cGUgL1BhZ2UKICAvUGFyZW50IDIgMCBSCiAgL1Jlc291cmNlcyA8PAogICAgL0ZvbnQgPDwKICAgICAgL0YxIDQgMCBSIAogICAgPj4KICA+PgogIC9Db250ZW50cyA1IDAgUgo+PgplbmRvYmoKCjQgMCBvYmoKPDwKICAvVHlwZSAvRm9udAogIC9TdWJ0eXBlIC9UeXBlMQogIC9CYXNlRm9udCAvVGltZXMtUm9tYW4KPj4KZW5kb2JqCgo1IDAgb2JqICAlIHBhZ2UgY29udGVudAo8PAogIC9MZW5ndGggNDQKPj4Kc3RyZWFtCkJUCjcwIDUwIFRECi9GMSAxMiBUZgooSGVsbG8sIHdvcmxkISkgVGoKRVQKZW5kc3RyZWFtCmVuZG9iagoKeHJlZgowIDYKMDAwMDAwMDAwMCA2NTUzNSBmIAowMDAwMDAwMDEwIDAwMDAwIG4gCjAwMDAwMDAwNzkgMDAwMDAgbiAKMDAwMDAwMDE3MyAwMDAwMCBuIAowMDAwMDAwMzAxIDAwMDAwIG4gCjAwMDAwMDAzODAgMDAwMDAgbiAKdHJhaWxlcgo8PAogIC9TaXplIDYKICAvUm9vdCAxIDAgUgo+PgpzdGFydHhyZWYKNDkyCiUlRU9G";
</script>

<PdfViewer url='./helloworld.pdf' />
<PdfViewer data={atob(base64)} />

```

## Props

| prop name | type | default | Required |
| ------------- | --------- | ------- | -------- |
| `url` | `string` | `""` | `Yes` |
| `url` | `string` | `N/A` | `Yes` |
| `data` | `string` | `N/A` | `No` |
| `scale` | `float` | `1.8` | `No` |
| `pageNum` | `number` | `1` | `No` |
| `flipTime` | `number` | `120` | `No` |
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "svelte-pdf",
"version": "1.0.12",
"version": "1.0.13",
"description": "svelte-pdf provides a component for rendering PDF documents using PDF.js",
"main": "src/index.js",
"main": "index.js",
"type": "module",
"scripts": {
"dev": "vite dev",
Expand Down
12 changes: 6 additions & 6 deletions src/lib/PdfViewer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import Tooltip from "./utils/Tooltip.svelte";
export let url;
export let data;
export let scale = 1.8;
export let pageNum = 1; //must be number
export let flipTime = 120; //by default 2 minute, value in seconds
Expand Down Expand Up @@ -148,7 +149,11 @@
*/
const initialLoad = async () => {
let loadingTask = pdfjs.getDocument({ url, password });
let loadingTask = pdfjs.getDocument({
...(url && { url }),
...(data && { data }),
...(password && { password }),
});
loadingTask.promise
.then(async function (pdfDoc_) {
pdfDoc = pdfDoc_;
Expand All @@ -172,11 +177,6 @@
.catch(function (error) {
passwordError = true;
passwordMessage = error.message;
if (passwordMessage === "Failed to fetch") {
//This API enables cross-origin requests to anywhere.
url = `https://cors-anywhere.herokuapp.com/${url}`;
initialLoad();
}
});
};
initialLoad();
Expand Down
2 changes: 1 addition & 1 deletion src/routes/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
import PdfViewer from '$lib/PdfViewer.svelte';
</script>

<PdfViewer url='https://nearlaw.com/PDF/MumbaiHC/2018/2018(7)-ALL-MR-95.pdf' />
<PdfViewer url='https://raw.githubusercontent.com/vinodnimbalkar/svelte-pdf/369db2f9edbf5ab8c87184193e1404340729bb3a/public/sample.pdf' />

0 comments on commit 7962eed

Please sign in to comment.