Need to add font-family dot matrix dynamically using jquery for print

2.5k Views Asked by At

I have a print function in jquery as follows:

$("#btn_print").click(function () {
        //Create print div
        populatePrintdiv();
        $("#btn_cmplte").css('display', 'block');
        var divToPrint = document.getElementById('printdiv');
        var newWin = window.open('', 'Print-Window');
        newWin.document.open();
        newWin.document.write('<html><body onload="window.print()" style="font-family:consolas;margin-left:30px;">' + divToPrint.innerHTML + '</body></html>');
        newWin.document.close();
        setTimeout(function () {
            newWin.close();
        }, 10);
    });

Here I am specifying font-family as consolas for as you can see in this line

newWin.document.write('<html><body onload="window.print()" style="font-family:consolas;margin-left:30px;">' + divToPrint.innerHTML + '</body></html>');

I want to use Dot Matrix font family so I downloaded the font family 1979_dot_matrix.ttf file and put it inside my project fonts folder.

How can I include this font-family using font face inside :

  newWin.document.write('<html><body onload="window.print()" style="font-family:consolas;margin-left:30px;">' + divToPrint.innerHTML + '</body></html>');

In css it will looks like:

@font-face {
  font-family: 1979_dot_matrix;
  src: local("1979_dot_matrix"),
       local("1979_dot_matrix"),
       url(fonts/1979_dot_matrix.ttf);
  font-weight: bold;
}

How can I use this dynamically using jquery as the same line above.

1

There are 1 best solutions below

0
On

I got the solution as:

newWin.document.write('<html><style>@font-face {font-family: DotMatrix;src: url("fonts/1979_dot_matrix.ttf");}</style><body onload="window.print()" style="font:1em DotMatrix;margin-left:30px;">' + divToPrint.innerHTML + '</body></html>');