Inconsistent Data on multiple page printing on a printer

谁说我不能喝 提交于 2019-12-06 04:30:36

I forked your plunker and I use a queue to allow multiple printing https://plnkr.co/edit/xZpcx6rCAUo9SemUPTt5?p=preview

I have a print function

function print(data) {
var domClone = '<div id="printThisElement" class="onlyprint" >'+
  '<table>'+
    '<tr> '+
        '<td>{{ data.instCode }}</td>'+
        '<td align="center">{{ data.date}}  </td>'+
    '</tr>'+
    '<tr> '+
        '<td colspan="2" align="center"> <img ng-src="data:image/JPEG;base64,{{data.barCodeImage}}"> </td>'+

    '</tr>'+
    '<tr> '+
        '<td colspan="2" align="center">{{ ctrl.user.name }} </td>'+
    '</tr>'+
        '<tr> '+
        '<td >Reg Id: {{ data.regIdLookup }}</td>'+
        '<td align="center">{{ data.testName }}</td>'+
    '</tr>'+

  '</table>'+
'</div>'
printSection.innerHTML = '';
  var scope = $rootScope.$new();
  scope.data = data;
  var domTemp = $compile(domClone)(scope)[0];
  printSection.appendChild(domTemp);
 $timeout(function(){
   onPrintFinished(window.print());  
 }, 0);
 }

And in PrintElement function i put in queu if printing is in progress :

if(!printInProgress) {
 printInProgress = true;
 print(data)
 }
 else {
   queue.push(data);
 }

At the end of printing run new printing with new data:

function onPrintFinished (printed){
var next = queue.shift();
if(next) {
  console.log(next, queue);
  $timeout(function() {
  print(next);
});
}
else {
  printInProgress = false;
}
}

I hope this time you have that you want

The problem is that vm.barCodeImage is set before corresponding PrintService.printElement is actually executed. So the sequence is:

  1. vm.barCodeImage = angular.copy(first entry);
  2. vm.barCodeImage = angular.copy(second entry);
  3. vm.barCodeImage = angular.copy(third entry);
  4. PrintService.printElement("printThisElement");
  5. PrintService.printElement("printThisElement");
  6. PrintService.printElement("printThisElement");

The solution is to modify your code in the following way:

$timeout(function() {
    vm.barCodeImage = angular.copy(entry);
    PrintService.printElement("printThisElement");
}, 0);

Thanks to that each call PrintService.printElement will use proper data and not the last element in the array.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!