Skip to content

Commit edc4045

Browse files
committed
Add attribute to limit the canvas-height to its container
This patch adds an attribute that, combined with `scale='page-fit'`, allows to fit the contents inside of the page both with respects to the width and with respect to the height. Think of it as using `background-size='contain'` rather than the default behavior of `background-size='cover'` that `scale='page-fit'` triggers.
1 parent be1ab8c commit edc4045

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

dist/angular-pdf.js

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
var pdfDoc = null;
4242
var pageToDisplay = isFinite(attrs.page) ? parseInt(attrs.page) : 1;
4343
var pageFit = attrs.scale === 'page-fit';
44+
var limitHeight = !!attrs.limitcanvasheight;
4445
var scale = attrs.scale > 0 ? attrs.scale : 1;
4546
var canvasid = attrs.canvasid || 'pdf-canvas';
4647
var canvas = document.getElementById(canvasid);
@@ -73,6 +74,9 @@
7374
viewport = page.getViewport(1);
7475
var clientRect = element[0].getBoundingClientRect();
7576
pageWidthScale = clientRect.width / viewport.width;
77+
if (limitHeight) {
78+
pageWidthScale = Math.min(pageWidthScale, clientRect.height / viewport.height);
79+
}
7680
scale = pageWidthScale;
7781
}
7882
viewport = page.getViewport(scale);

readme.md

+5
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ Check [`bower.json` file](https://github.com/sayanee/angularjs-pdf/blob/master/b
6969
```html
7070
<ng-pdf template-url="/partials/viewer.html" scale="page-fit"></ng-pdf>
7171
```
72+
- `limitcanvasheight` as an option combined with `scale='page-fit'`
73+
74+
```html
75+
<ng-pdf template-url="/partials/viewer.html" scale="page-fit" limitcanvasheight="1"></ng-pdf>
76+
```
7277
- `page` as an option for initial page number
7378

7479
```html

0 commit comments

Comments
 (0)