Search code examples
javascripthtmlcssprintingjsbarcode

Barcode not printing correctly using JSBarcode library


I am trying to print a generated barcode. I am using the JSBarcode library to generate the barcode.

On the print screen, visuality is good but when I print it, the result is 4 pages and just one page has the barcode, and the barcode is very little and at the bottom of the page.

My html code is:

<div class="col-12 col-print-12">
   <p class="barcode tekli">
      <svg id="barcode tekli"></svg>
   </p>
</div>

My JS code is:

JsBarcode("#barcode", "1554466", {
    width:5,
    height:200,
    fontSize:50,
    displayValue: true
});

window.print();

window.open('', '_parent', '');

window.close();

My CSS is:

@media print {
    .tekli{
        -webkit-transform: scaleX(2);
        -o-transform: scaleX(2);
        -ms-transform: scaleX(2);
        transform: scaleX(2);
    }
    .page {
        width: 21cm;
        height: 29.7cm;
        margin: 0;
        border: initial;
        border-radius: initial;
        width: initial;
        min-height: initial;
        box-shadow: initial;
        background: initial;
        page-break-after: always;
    }
    .col-print-12 {
        width: 100%;
    }
}

Can you help me please?


Solution

  • Try this

    $(document).ready(function(){
        JsBarcode("#barcode", "1554466",{
            width:5,
            height:200,
            fontSize:50,
            displayValue: true
    
        });
    
        window.print();
    
        window.open('', '_parent', '');
    
        window.close();
    });
    

    or to delete svg tag and replace with a simple div with id="barcode"

    let divDOM = document.getElementById("barcode");
    let svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
    svg.setAttribute('jsbarcode-format', 'ean13')
    svg.setAttribute('jsbarcode-value', '<?php echo $product["ean"]; ?>')
    svg.className.baseVal = "barcode";
    divDOM.appendChild(svg);
    JsBarcode("#barcode","Smallest width",{
        width: 1,
        height: 25,
        lineColor: "#676a6c"
    }).init();