0

I'm trying to print a simple table, yet the layout seems to mess up. Does anybody know why?

Table to print

enter image description here

Print Preview

enter image description here

Here is the HTML and JS I have used to create the Table.

function printData() {
  var divToPrint = document.getElementById("printtable");
  newWin = window.open("");
  newWin.document.write(divToPrint.outerHTML);
  newWin.print();
  newWin.close();
}

$('button').on('click', function() {
  printData();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="printtable" style="border: 1px solid black; width: 90mm; height: 29mm;">
  <div style="float: left; font-size: 10px; position: relative; top: 50%; transform: translateY(-50%); margin-left: 2mm;">
    <p>
      <img src="/" alt="" height="30">
    </p>
    <table>
      <tbody>
        <tr>
          <td><u>Row 1</u>&nbsp;:</td>
          <td>&nbsp; Row 1</td>
        </tr>
        <tr>
          <td><u>Longer Row 2</u>&nbsp;:</td>
          <td>&nbsp; Row 2</td>
        </tr>
        <tr>
          <td><u>R3</u> :</td>
          <td>&nbsp; Row 3</td>
        </tr>
      </tbody>
    </table>
  </div>
</div>
<br />
<br />

<button>Print me</button>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Peter Raeves
  • 516
  • 1
  • 4
  • 17

2 Answers2

0

You need to specify style related to print .

You can use @media print to specify styles for print

brk
  • 48,835
  • 10
  • 56
  • 78
0

In the end, I used the solution proposed here is such alignment achievable without <table>?

By aligning my text without using the table tag, the text prints just fine.

Community
  • 1
  • 1
Peter Raeves
  • 516
  • 1
  • 4
  • 17