openhtmltopdf icon indicating copy to clipboard operation
openhtmltopdf copied to clipboard

Problem with page margins when using @page:first rule

Open Bigdatha opened this issue 7 years ago • 4 comments

Hello,

I am trying to create a PDF file which has a kind of banner on the right side of the first page.

So i used the @page, @page:first and the @right-top rules to adjust the page margins and to insert the additional content:

@page {
  size: A4 portrait;
  margin: 10mm 5mm 15mm 20mm;
}

@page:first {
  margin-right: 55mm;
  @right-top {
    padding: 1mm;
    content: 'Some additional content\00000a@right-top\00000a(but only on the first page)';
    white-space: pre;
    vertical-align: top; text-align: left;
    background: #ffeeee;
  };
}

This works perfect for the first page, but on the follwing ones, the right margin defined in the @page:first rule is still used instead of the default value defined in the @page rule (by the way, similar effects arise when using the @page:left or @page:right rules).

For clarification please have a look at Template.html.txt and Template.pdf

Thanks, Bigdatha

Bigdatha avatar Aug 23 '17 09:08 Bigdatha

Hi @Bigdatha

Thanks for this report. I finally tracked down the cause. It turns out we set the size of the body to the content width of the first page. You can see what is happening by putting a border on the body element.

Unfortunately, I don't think we can change this behaviour due to backwards compatibility. Even if we could, I'm not sure of the correct behaviour because the body can only have one size. Different effective page sizes are problematic in HTML because of the nature of fixed width blocks.

Possibly a workaround, for your specific case, is to use a floating element on the first page or a table.

Let me know any thoughts.

danfickle avatar Sep 09 '17 05:09 danfickle

Hi @danfickle ,

first of all, thank you very much for investigating this issue.

I picked up your suggestion using a floating container, this works fine except a minor problem with the first pagebreak (if you are interested, you can find an example in this zipfile).

I'm not sure of the correct behaviour because the body can only have one size.

Just thinking ... should or can paged media be handled like a single paged webpage ?

For paged media, we can define the size and margins for the whole document in the @page rule, moreover we can define other margins and sizes for the :first, :left and :right page(s).

As a result, the available space for printing the content can differ a lot from page to page.

I don't know if it is possible at all, but i think for paged media the size of the body element has to be calculated seperately for each page ... at least i can not imagine another way to get the expected behaviour.

Regards, Bigdatha

Bigdatha avatar Sep 15 '17 12:09 Bigdatha

As #376 is closed I am posting this here: Can a page format change not be coupled to explicit page breaks? I see where different page widths might complicate automatic page wrapping, but explicit page splits introduce hard transitions anyway. What is the difference of this to splitting the HTML accordingly and generating the page series separately until the next page break with a format change, while just carrying over the page counter?

hbergmey avatar May 11 '20 13:05 hbergmey

hi, is this feature being developed in progress or any info about this?

nealsun avatar Dec 06 '21 06:12 nealsun