Skip to content

Commit

Permalink
Added ability to select initial page number (#17)
Browse files Browse the repository at this point in the history
* Added ability to select inital page number

* Resolved merge conflicts after rebase

Co-authored-by: Anvith KS <[email protected]>
  • Loading branch information
jeremybohannon and anvithks authored Aug 29, 2021
1 parent 1b62c34 commit e309a62
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ To hide pagination
{{< embed-pdf url="./path/to/pdf/file/example.pdf" hidePaginator="true" >}}
```


To render a selected page number
```
{{< embed-pdf url="./path/to/pdf/file/example.pdf" renderPageNum="5" >}}
```

To hide loading spinner
```
{{< embed-pdf url="./path/to/pdf/file/example.pdf" hideLoader="true" >}}
Expand All @@ -75,7 +81,9 @@ To hide loading spinner
### Parameters
- **url (required)** : The relative location of the file.
- **hidePaginator (optional)**: Boolean which expects `true` or `false`. Hides the paginator for single page documents.
- **renderPageNum (optional)**: Integer which expects any number from `1` up to the last page number in the document. Will render that specific page on initial load.
- **hideLoader (optional)**: Boolean which expects `true` or `false`. Hides the loading spinner while your document loads.

<br />

**Note:** Currently supports local file embed. If absolute URL from the remote server is provided, configure the CORS header on that server.
Expand Down
12 changes: 10 additions & 2 deletions layouts/shortcodes/embed-pdf.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@
// If absolute URL from the remote server is provided, configure the CORS
// header on that server.
var url = "{{.Site.BaseURL}}" + '{{ .Get "url" }}';

var hidePaginator = "{{ .Get "hidePaginator" }}" === "true";
var hideLoader = "{{ .Get "hideLoader" }}" === "true";
var selectedPageNum = parseInt("{{ .Get "renderPageNum" }}") || 1;

// Loaded via <script> tag, create shortcut to access PDF.js exports.
var pdfjsLib = window['pdfjs-dist/build/pdf'];
Expand All @@ -70,7 +72,7 @@

// Change the Scale value for lower or higher resolution.
var pdfDoc = null,
pageNum = 1,
pageNum = selectedPageNum,
pageRendering = false,
pageNumPending = null,
scale = 3,
Expand Down Expand Up @@ -186,7 +188,13 @@
*/
pdfjsLib.getDocument(url).promise.then(function(pdfDoc_) {
pdfDoc = pdfDoc_;
document.getElementById('page_count').textContent = pdfDoc.numPages;
var numPages = pdfDoc.numPages;
document.getElementById('page_count').textContent = numPages;

// If the user passed in a number that is out of range, render the last page.
if(pageNum > numPages) {
pageNum = numPages
}

// Initial/first page rendering
renderPage(pageNum);
Expand Down

0 comments on commit e309a62

Please sign in to comment.