1

hello friends i'm using vue.js to create a table and i want a button to print the data in the table without the preview dialog so what should i write my javasript code in vue. this is my table: enter image description here

and this is my print button code

 Print: function(){
  var mycocument=document.getElementById('table_users');
  var newWin=window.open("","");
  newWin.document.open();
  newWin.document.write(mycocument.outerHTML);
  newWin.print();
  newWin.close();
}
  • The print dialogue is there for a reason, don't try and avoid it. What if I want to change printers, select greyscale printinng, or adjust any other settings. I also seriously doubt this is possible as javasctipt, by design, has only a very limited amount of control over the browser. – Jon P Jan 07 '20 at 03:26

2 Answers2

0

OPTION 1
You can try doing it this way.

function printDiv() { 
            var divContents = document.getElementById("table_users").innerHTML; 
            var a = window.open('', '', 'height=500, width=500'); 
            a.document.write('<html>'); 
            a.document.write('<body > <h1>This is a sample print</h1> <br>'); 
            a.document.write(divContents); 
            a.document.write('</body></html>'); 
            a.document.close(); 
            a.print(); 
        } 

you can check this example https://codepen.io/misterjenuel/pen/OJPzqVL

OPTION 2
You can do it using https://github.com/MrRio/jsPDF.

*****html*****

<div id="content">
     <h3>Hello, this is a H3 tag</h3>
     <table border="1px" style="border-collapse: collapse;"> 
            <tr> 
                <td>computer</td> 
                <td>Algorithm</td> 
            </tr> 
            <tr> 
                <td>Microwave</td> 
                <td>Infrared</td> 
            </tr> 
        </table> 
</div>
<div id="editor"></div>
<button id="cmd">generate PDF</button>

*****JavaScript:*****

var doc = new jsPDF();
var specialElementHandlers = {
    '#editor': function (element, renderer) {
        return true;
    }
};

$('#cmd').click(function () {
    doc.fromHTML($('#content').html(), 15, 15, {
        'width': 170,
            'elementHandlers': specialElementHandlers
    });
    doc.save('sample-file.pdf');
});

OPTION 3
Content inside a .... can be downloaded as pdf with styles using jspdf & html2canvas.

You need to refer both js libraries,

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js"></script>
<script type="text/javascript" src="https://html2canvas.hertzen.com/dist/html2canvas.js"></script>

Then call below function,

//Create PDf from HTML...
function CreatePDFfromHTML() {
    var HTML_Width = $(".html-content").width();
    var HTML_Height = $(".html-content").height();
    var top_left_margin = 15;
    var PDF_Width = HTML_Width + (top_left_margin * 2);
    var PDF_Height = (PDF_Width * 1.5) + (top_left_margin * 2);
    var canvas_image_width = HTML_Width;
    var canvas_image_height = HTML_Height;

    var totalPDFPages = Math.ceil(HTML_Height / PDF_Height) - 1;

    html2canvas($(".html-content")[0]).then(function (canvas) {
        var imgData = canvas.toDataURL("image/jpeg", 1.0);
        var pdf = new jsPDF('p', 'pt', [PDF_Width, PDF_Height]);
        pdf.addImage(imgData, 'JPG', top_left_margin, top_left_margin, canvas_image_width, canvas_image_height);
        for (var i = 1; i <= totalPDFPages; i++) { 
            pdf.addPage(PDF_Width, PDF_Height);
            pdf.addImage(imgData, 'JPG', top_left_margin, -(PDF_Height*i)+(top_left_margin*4),canvas_image_width,canvas_image_height);
        }
        pdf.save("Your_PDF_Name.pdf");
        $(".html-content").hide();
    });
}

Ref: https://www.freakyjolly.com/jspdf-multipage-example-generate-multipage-pdf-using-single-canvas-of-html-document-using-jspdf/

Jenuel Ganawed
  • 358
  • 6
  • 15
0

BUT if your trying to directly print it:

I came across another elegant solution for this:

Place your printable part inside a div with an id like this:

<div id="printableArea">
      <h1>Print me</h1>
</div>

<input type="button" onclick="printDiv('printableArea')" value="print a div!" />

Now let's create a really simple javascript:

function printDiv(divName) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;
}

SOURCE: https://stackoverflow.com/a/7532581/1223045

Jenuel Ganawed
  • 358
  • 6
  • 15