dompdf HTML to PDF - can't set margin of page
Asked Answered
T

3

30

version: 0.6.0 beta 3

I tryed in every manner to make dompdf set the margin of the page. I have a long page with a lot of text, divided in chapters...

my css is something like:

#wrapper{
    padding:8px;
}

/* other styles... */

@page{margin: 0.2in 0.5in 0.2in 0.5in;}

when the php is

<?php
ob_start(); // begin collecting output
include 'makemypdf.php'; // this page output the html
$html = ob_get_clean(); // retrieve output from makemypdf.php and stop buffering


require_once("dompdf/dompdf_config.inc.php");

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf", array("Attachment" => false));

?>

but what I get is a page with NO margins!!! only the padding of the #wrapper are applied... and they are applyed only at the beginning and at the end of the entire PDF...

I'm doing something wrong?

PS - it seems that only the bottom-margin is applied... but I'm not sure...

PPS - I tryed with no success also this css: body { margin-top: 40px; } html { margin-top: 40px; } @page { margin-top: 40px; }

Tablecloth answered 4/11, 2013 at 23:30 Comment(2)
try using <tag style="margin: 0.2in 0.5in 0.2in 0.5in;"></tag> as <body style="margin: 0.2in 0.5in 0.2in 0.5in;"></body>Caryloncaryn
thank you for your answer! I figured out what I "was doing wrong" after a few steps today. I've post the solution.Tablecloth
T
55

I figured out that neither body or @page works in this version of dompdf.

the problem was in the main CSS, where I put every tyme this line:

*{margin:0;padding:0}

I find out that margins of the PDF are decided in base of the margin of the HTML, so I removed that line with the global selector and replaced with:

th,td,p,div,b ... {margin:0;padding:0}
html{margin:40px 50px}

This works as "un-expected" and I get the right margin in every page.

Tablecloth answered 5/11, 2013 at 9:34 Comment(3)
There does appear to be an issue with using * and attempting to set page margins. We'll need to look into it. You can still use the * selector, just force the page margins by appending !important, e.g. @page { margin: 40px 50px !important; }.Awhile
FYI, dompdf considers the HTML element and @page to be the same. There is a bug in v0.6.0 beta 3 where you had to include a space after the @page selector. It has been fixed for the next release.Awhile
You're welcome. I need to amend my statement about @page vs. the HTML element. I don't think they're the same, but affect the document in similar ways. We'll need to decide if we want those two selectors to represent the same entity.Awhile
S
11

This fixed my problem:

        @page {
            margin: 0 !important;
            padding: 0 !important;
        }
Sara answered 13/2, 2019 at 8:12 Comment(0)
A
2

You can do it on body tag, like that : body { margin: 0.2cm; }

Acephalous answered 9/6, 2023 at 15:8 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.