PDF are displayed mirror-reflected and vertical diverted, when using pdf.js in AngularJS application
See original GitHub issueHi,
I’ve got similar issue corresponding to previously reported #3665, but it occurs in quite different enviroment, so I’ve created separate issue for this.
Details: I use pdf.js in some application. HTML5 views are generated by Java-based backend (JSP, Spring), where I’ve integrated AngularJS to manipulate client-side code directly in web browser.
I’ve got an Angular service, which is simple wrapper for pdf.js basic functionality:
application.service("PdfjsService", [ function() {
// reference to service for callbacks
var __service = this;
this.url = "";
this.canvasId = "";
// initial values
this.page = 1;
this.scale = 1.0;
this.init = function(canvasId, page, scale) {
this.canvasId = canvasId;
if (scale) {
this.scale = scale;
}
if (page) {
this.page = page;
}
};
this.render = function(url) {
if (PDFJS) {
PDFJS.getDocument(url).then(function(pdf) {
pdf.getPage(__service.page).then(function(page) {
var scale = __service.scale;
var viewport = page.getViewport(scale);
var canvas = document.getElementById(__service.canvasId);
var context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
var renderContext = {
canvasContext: context,
viewport: viewport
};
page.render(renderContext);
});
});
}
};
}]);
I can call this service in two different ways:
- only in controller, like this:
// other code in Angular controller
PdfjsService.init('the-canvas', 1, 1);
PdfjsService.render('${contextPath}/resources/img/somefile.pdf');
// other code in Angular controller
or
- directly in JSP HTML5 code, after passing reference to PdfjsService as $scope var:
controller:
// other code in Angular controller
PdfjsService.init('the-canvas', 1, 1);
$scope.pdfservice = PdfjsService;
// other code in Angular controller
and view:
<!-- other HTML -->
<div class="span4">
<canvas id="the-canvas" style="border:1px solid black; width:350px;"/>
</div>
{{pdfservice.render('${contextPath}/resources/img/somefile.pdf')}}
<!-- other HTML -->
${contextPath} is Spring variable, which has no impact for whole situation (it’s only a part of path to pdf file).
Pdf file is visible in both cases, only in the second one is diverted vertical and mirrored 😕
I can’t attach this specific pdf file, because it’s confidential (it’s a part of commercial project and only for internal use)
Issue Analytics
- State:
- Created 10 years ago
- Comments:11 (4 by maintainers)
Top GitHub Comments
Looks like the issue was resolve. Closing.
The “mirrored” canvas display happens:
To avoid this, don’t (re)use the canvas until the page rendering is finished or canceled.
@yurydelendik I am facing an identical issue myself. I’m working on an AngularJS application where one of the core requirements is to view and sign off PDFs.
At the moment I’ve got a working PDF that I’m using as a placeholder which I reference by its path. It displays fine in any traditional PDF-reader, but when I attempt to reference it in Angular the pdf comes out distorted.
I’ve used an identical implementation as the onPrevNext that you referred Varun to:
`(function () {
})();`
The code runs intended with no errors (but some small warnings regarding the arraybuffer:
I’d love to hear your thoughts about this. You can find the example on our website