phpdompdf

Is there a way to set margins to be different for DOMPDF on a per page basis (at least just the first page)


I was hoping to create a coverpage for my pdf but I can't figure it out.

I have DOMPDF successfully creating my pdf. I wanted a coverpage that is full bleed, no footer (I used the Canvas to create a footer with page count).

I was able to get the footer to be removed, but I can't figure out the margins.

In my css which is an external file, I can set

@page { 
    margin:0;
}

This makes the coverpage look great but it also removes margins from all the other pages. Which is not what I really want to happen. Is there a way to specify only the first page?

Looking through some other questions I noticed that you could call the current page via css. As an example:

.footer .page-number:after { 
    content: counter(page);
}

But I couldn't get any action based on using counter(page) as a selector.

I looked into rendering 2 $html sets together to make 1 pdf but the DOMPDF notes say that's not possible and could cause some rendering errors.

I'm using DOM 3.0.0 CPDF in a WordPress installation.


Solution

  • CSS provides a :first pseudo class, that can be used in combination with a @page rule - and apparently dompdf supports it.

    https://developer.mozilla.org/en-US/docs/Web/CSS/:first :

    The :first CSS pseudo-class, used with the @page at-rule, represents the first page of a printed document.

    There's a little caveat,

    Note: You can't change all CSS properties with this pseudo-class. You can only change the margins, orphans, widows, and page breaks of the document. Furthermore, you may only use absolute-length units when defining the margins. All other properties will be ignored.

    - but that should not be relevant for your purpose, since you want to set different margins only.