javascriptprintjs

How to add custom JavaScript file on printJS


I have a script for printing the receipt and it works perfectly fine as well as the CSS file

printJS({printable:'print-receipt', type:'html',css:'assets/css/receipt.css'});

But, when I try to add a JavaScript file with this following code i.e: javascript:'charts/charts.min.js' and it should look like this

printJS({printable:'print-receipt', type:'html',css:'assets/css/receipt.css',javascript:'charts/charts.min.js'});

Then, there is no effect after the javascript file is added! Is there any way to add a javascript file on printJS ? Please, I need your help. Thanks in advance :)


Solution

  • From the documentation page (https://printjs.crabbly.com/) there's no argument javascript that's why it doesn't work.

    Since the printable argument works with html as well, i suggest you to create that element first.

    <canvas id="chart"></canvas>
    <!-- This is the library of chartjs -->
    <script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>
    

    and in js make sure you select the chart element

    printJS({printable:'chart', type:'html'});
    

    this is the sample implementation : https://jsfiddle.net/oak8Ljd6/