John Jerald De Chavez

HTML to PDF

I'll be showing some tips on how to work with HTML to PDF

Every new page add space

We can use the psuedo class to filter the not first-child when we are trying to add padding-top.

.perpage:not(:first-child) {
  padding-top: 2cm;
}

Break page after when is not last child

Imagine we need to render multiple pages of pdf. Currently what I implement that I need to return a new html that separate from the page. Something like this:

<html> ... </html>

<html> ... </html>

What we can do is by adding page-break-after with value of always !important; and implement when child is not last with css psuedo class.

.pagebreakafter:not(:last-child) {
  page-break-after: always !important;
}

Styling your footer

Add fixed footer style by adding position value fixed; and add some padding for your preference.

@media print {
  .footer {
    position: fixed;
    bottom: 0;
    right: 0;
    left: 0;
    text-align: right;
    padding: 1rem;
  }
}

References