Cannot read property 'getContext' of null
I'm having the same problem as a closed issue, but doesn't seem to be cross domain problem with the PDF. When running my app I get the error
TypeError: Cannot read property 'getContext' of null
Which I assume means it can't get the canvas. Template file looks like this:
<ng-pdf template-url="partials/viewer.html" canvasid="pdf-canvas" scale="page-fit"></ng-pdf>
viewer.html content looks like this:
<canvas ng-if="pdfUrl" id="pdf-canvas" class="rotate0"></canvas>
And lastly the controller:
$ionicModal.fromTemplateUrl('templates/pdf-viewer.html', {
scope: $scope,
animation: 'slide-in-up'
}).then(function (modal) {
$scope.modal = modal;
});
$scope.viewPDF = function() {
$scope.pdfUrl = 'http://localhost/commonproperty/bin/temp.pdf';
$scope.modal.show();
};
I've tried to reference a PDF in the app root but still get the same error.
Any help would be greatly appreciated!
Hi @brettmeyer, this might happen when the canvas is not available in the DOM (the directive is using document.getElementById() to find the canvas. I assume your canvas is located in a modal view or something similar, which hasn't been loaded yet. Either modify the order of function execution or preload the canvas.
@brettmeyer does the suggestion of @dani-inside solves the issue?
@dennybiasiolli to be honest I can't remember what I did to resolve this, which I did before there was a response, but I think @dani-inside was correct. This is what the code looks like in the working app:
$scope.showPDF = function(filename) {
$scope.pdfUrl = filename;
$scope.showPDFModal('templates/pdf-viewer.html');
};
$scope.showPDFModal = function(templateUrl) {
$ionicModal.fromTemplateUrl(templateUrl, {
scope: $scope
}).then(function(modal) {
$scope.modal = modal;
$scope.modal.show();
});
};
And the contents of pdf-viewer.html:
<script id="viewer.html" type="text/ng-template">
<div class="button-bar">
<a class="button" ng-click="goPrevious()"><i class="ion-ios-arrow-left"></i></a>
<a class="button" ng-click="zoomOut()"><i class="ion-ios-minus-outline"></i></a>
<a class="button" ng-click="fit()"><i class="ion-ios-search"></i></a>
<a class="button" ng-click="zoomIn()"><i class="ion-ios-plus-outline"></i></a>
<a class="button" ng-click="goNext()"><i class="ion-ios-arrow-right"></i></a>
</div>
<ion-scroll direction="xy" scrollbar-x="false" scrollbar-y="false" zooming="true" min-zoom="1" style="width: 100%; height: 100%;">
<canvas id="pdf" class="rotate0"style="width: 100%; height: 100%;"></canvas>
</ion-scroll>
</script>
<ion-modal-view>
<ion-header-bar>
<h1 class="title">PDF</h1>
<button ng-click="closeModal()" class="button button-icon icon ion-android-close"></button>
</ion-header-bar>
<ion-content>
<ng-pdf template-url="viewer.html" canvasid="pdf" scale="page-fit"></ng-pdf>
</ion-content>
</ion-modal-view>
@brettmeyer thanks for your response. 😄
I want to add something to the useful explanation by @brettmeyer: I implemented the same thing (PDF preview in a $ionicModal) and I had to make sure to call modal.remove() when closing it. Otherwise, when reopening the PDF would not be rendered correctly. Just thought it may be useful to someone struggling on the same problem as me in the last two hours!