Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fit a html table in A4 size

Tags:

html

css

pdf

I have a html file with a wide table. I want to be able to put it in a A4 size. The columns that exceed the A4 size should come below in a new table.

I tried using this the @page attribute, but it didn't change anything,

<style type="text/css">
    @page {    size: 21cm 29.7cm; margin: 30mm 45mm 30mm 45mm; }
</style>

Is there any third party js library that automatically does this? (The table size is not known before hand, the user uploads data and generates the table, so the number of columns is not fixed). {My end goal is to print this as a pdf, but I could not achieve this using the qprinter given in QT}

I have put a html file with long table here - link.

Thanks in advance

like image 553
clearScreen Avatar asked Apr 07 '16 11:04

clearScreen


People also ask

How to set table width in HTML?

How to set table width in HTML? To set the table width in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <table> tag, with the CSS property width.

What is the max size of a table in A4 page?

A4 page width in px is 2480 pixels x 3508 pixels. so you will make max-width of table to 2480px so that it could not exceed the size of paper. Let's guess your table id="table", then your style should be. Show activity on this post. You should try to use divs rather than tables if you want to get good printable sheets.

How to set the size of a specific column in HTML?

Note: Using a percentage as the size unit for a width means how wide will this element be compared to its parent element, which in this case is the <body> element. To set the size of a specific column, add the style attribute on a <th> or <td> element:

What is the page width of a table in px?

A4 page width in px is 2480 pixels x 3508 pixels. so you will make max-width of table to 2480px so that it could not exceed the size of paper. Let's guess your table id="table", then your style should be.


1 Answers

Table could not break its columns in a new row, all you can do is to make table width according to the paper width.

A4 page width in px is 2480 pixels x 3508 pixels.

so you will make max-width of table to 2480px so that it could not exceed the size of paper.

Let's guess your table id="table", then your style should be.

<style>
  #table{
    max-width: 2480px;
    width:100%;
  }
  #table td{
    width: auto;
    overflow: hidden;
    word-wrap: break-word;
  }
</style>
like image 118
Fawad Ali Avatar answered Sep 17 '22 12:09

Fawad Ali