coder.dash
coder.dash

Reputation: 33

Itext 7 overriding default margin in pdfHtml

I have html content that has to start at the absolute top left corner of the page. However, the HtmlConverter automatically adds a 0.5in gap from the top and left side of the pdf page. How do I override this default margin?

Upvotes: 2

Views: 3572

Answers (1)

Samuel Huylebroeck
Samuel Huylebroeck

Reputation: 1719

You can set the margins of a page through CSS using @page. The following declaration sets all the page-margins to 0, as well as draws a border around paragraphs for visual reference:

@page{
    margin:0pt;
}
p{
    border-left: solid 2pt blue;
    border-top: solid 1pt blue;
    border-bottom: solid 1pt blue;
}

Use the following as input:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>margin example</title>
    <link href="margin.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div>
        <p>This page should have the margins set at 0</p>
    </div>
</body>
</html>

And you'll see that the paragraph's border is touching the left side of the page, but not yet the top side. That's because the paragraph has some innate margin as a blockelement as well. Setting this to 0 will do the trick:

p{
    border-left: solid 2pt blue;
    border-top: solid 1pt blue;
    border-bottom: solid 1pt blue;
    margin-top: 0pt;
}

@page{
    margin:0pt;
}

And the output:

margins

Upvotes: 10

Related Questions