Print button for printing multiple pages

I am trying to have a print button that will print multiple PDF files or files when clicked.

Is it possible to have a "Print All" button:

<button class="btn-u btn-u-orange" name="printall" id="printall" ><i class="icon-printer"></i> Print All</button>

But then somehow it will print all pdf files or pages when clicked. I tried to do this using javascript, but unfortunately I failed.

    function PrintAll() {
        var pages =  ["forms/82040PDFCreator.cfm", "forms/poa.cfm", "forms/Billofsalevehicle.cfm"];
        for (var i = 0; i < pages.length; i++) {
            var oWindow = window.open(pages[i], "print");
            oWindow.print();
            oWindow.close();
        }
    }

$("#printall").on("click",function(){
        PrintAll();
    });

When I select print, it only prints the first PDF to print and nothing more. Any help with this would be greatly appreciated.

The coldfusion tag is added because I call .cfms which fill the PDF files.

What Iv tried to create iframes:

<iframe src="forms/82040PDFCreator.cfm" style="width:400px; height:400px;" frameborder="0"></iframe>
<iframe src="forms/poa.cfm" style="width:400px; height:400px;" frameborder="0"></iframe>
<iframe src="forms/Billofsalevehicle.cfm" style="width:400px; height:400px;" frameborder="0"></iframe>
<iframe src="forms/IRF.cfm" style="width:400px; height:400px;" frameborder="0"></iframe>
<iframe src="forms/InsuranceAffidavit.cfm" style="width:400px; height:400px;" frameborder="0"></iframe>
<iframe src="forms/FeesBreakdown.cfm" style="width:400px; height:400px;" frameborder="0"></iframe>

. , , . , , , , . , - . , , , , , .

- - , :

$("#printall").on("click",function(){
  var iframes = document.getElementsByTagName("iframe");
  var i = 0;
  window.setInterval(function(){
      if(i < iframes.length){
        i++;
      }
  }, 3000);
    });
+4
2

Chrome 64:

function PrintAll() {
  var pages =  ["forms/82040PDFCreator.cfm", "forms/poa.cfm", "forms/Billofsalevehicle.cfm"];

  var printNext = function(i) {
    i = i || 0;
    if (i >= pages.length) {
      return;
    }

    var wdw = window.open(pages[i], 'print');
    wdw.onload = function() {
      wdw.print();

      wdw.close();
      setTimeout(function() { 
        printNext(++i);
      }, 100);

    }
  };

  printNext();
}

setTimeout , . - .

+2

iframe.

iframe . , . iframe . iframe setTimeout. / , :

function PrintAll() {
      var pages = ["forms/82040PDFCreator.cfm", "forms/poa.cfm", "forms/Billofsalevehicle.cfm"];
      for (var i = 0; i < pages.length; i++) {
        $('<iframe>', {
            name: 'myiframe',
            class: 'printFrame'
          })
          .appendTo('body');
        $('.printFrame').load(pages[i], function(response, status, xhr) {
          window.frames['myiframe'].focus();
          window.frames['myiframe'].print();

          //above two lines can be merged as:
          //window.frames['myiframe'].focus().print();

          setTimeout(() => {
            $(".printFrame").remove();
          }, 1000);
        });
      }
    }
+1

Source: https://habr.com/ru/post/1692672/


All Articles