Top Margin problem

3,808 views
Skip to first unread message

AL

unread,
May 5, 2011, 8:57:02 AM5/5/11
to dompdf
Hi all.
I'm trying to generate a pdf report starting from a mysql db and php
project. Actually I let users edit the content of the report,
initially grabbed from the db, through a WYSIWYG and post the WYSIWYG
html to dompdf to generate a the new report.

I've been usign dompdf for a couple of days and managed to add header
and footer on each page but the first one (it was a little bit tricky
but found the group of real support!).

My main problem now is I'm not able to set top margin for each page.

//$_POST['elm1'] contains html data to print

$to_print = "<head><style>@media all{ body{margin: 2cm 1cm 1cm 1cm;}}</
style></head><body>".$header.$footer.$_POST['elm1']."</body>";
$dompdf->load_html($to_print);
$dompdf->render();
$dompdf->stream($file_name, array("Attachment" => 0));

left, right and bottom margins work right. No matter what I try:Top
margin is completely skipped!

Can someone provide some help?

Thanks in advance

Martin Thomas Schrott

unread,
May 5, 2011, 9:55:38 AM5/5/11
to dom...@googlegroups.com
Hi,

just add the following to your css file/area:
body {

margin-top: 10px;

}

this will be used on all pages.

cheers

Martin

Thanks in advance

--
You received this message because you are subscribed to the Google Groups
"dompdf" group.
To post to this group, send email to dom...@googlegroups.com.
To unsubscribe from this group, send email to
dompdf+un...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/dompdf?hl=en.


AL

unread,
May 5, 2011, 10:12:08 AM5/5/11
to dompdf
Dear Martin,
thanks. I already tried this with no success...but I noticed a strange
behavior using margin-top=20cm (yes! 20cm).

The margin-top actually works BUT JUST for the FIRST page!
It does not work for all other pages: both those generated
automatically (with "page-break-after:always" tag inserted in elements
such as tables or <ul> I want to occupy just one page) and those
generated by dompdf because content is too long to fit a single
page...
For all pages but the first the top margin is always the same: ~1cm.

Any idea?????

Martin Thomas Schrott

unread,
May 5, 2011, 10:27:41 AM5/5/11
to dom...@googlegroups.com
hmmm. sounds strange.

do you use the newest version or at least beta 2 of 0.6?
i guess in older versions this did not work.

If you have the newest version... do you insert the css right into the
content or do you have an external stylesheet file?

I use external stylesheets and this does work without any problems there.
(you will have to activate external sources in the config)

beside that I do not have any other ideas. Sorry.

good luck!

AL

unread,
May 5, 2011, 11:03:34 AM5/5/11
to dompdf
I checked out the latest version from the trunk.
I'll try using external css and let you know.

Thanks

AL

unread,
May 5, 2011, 11:30:49 AM5/5/11
to dompdf
External CSS: first and second pages ok...other pages margin-top has
no effect at all!

This is the HTML I pass to dompdf:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<link href="styles/style_pdf.css" rel="stylesheet" type="text/css"/>
</head>
<body>

<script type="text/php">
if ( isset($pdf) )
{
$php_code =
'
$w = $pdf->get_width();
$h = $pdf->get_height();
$header = $pdf->open_object();

// Add a logo
$img_w = 45;
$img_h = 35;

$pdf->image("images/logo_little.png", "png", 60, 15,
$img_w, $img_h );

// Close the object (stop capture)
$pdf->close_object();

$font = Font_Metrics::get_font("Helvetica", "italic");
$size = 10;
$color = array(0.7,0.7,0.7); // grigio

$text_center_up = "Acceptance Test Plan";
$text_center_dw = "SOME TEXT HERE";

$width_center_up = Font_Metrics::get_text_width($text_center_up,
$font, $size);
$width_center_dw = Font_Metrics::get_text_width($text_center_dw,
$font, $size);

$text_right = "SOME PROJECT HERE1";
$width_right = Font_Metrics::get_text_width($text_right, $font,
$size);


if ($PAGE_NUM > 1)
{
$pdf->add_object($header, "all");
$pdf->text($w / 2 - $width_center_up / 2, 20, $text_center_up,
$font, $size, $color);
$pdf->text($w / 2 - $width_center_dw / 2, 35, $text_center_dw,
$font, $size, $color);
$pdf->text($w - $width_right - 60, 35, $text_right, $font,
$size, $color);
$pdf->line(60, 55, $w - 60, 55, $color, 0.5);
}
';
$pdf->page_script($php_code);
}



</script>
<script type="text/php">
if ( isset($pdf) )
{
$php_code =
'
$font = Font_Metrics::get_font("verdana");
if (!isset($font))
{
Font_Metrics::get_font("sans-serif");
}
$size = 10;
$color = array(0.7,0.7,0.7); // grigio
$text_height = Font_Metrics::get_font_height($font, $size);
$w = $pdf->get_width();
$h = $pdf->get_height();
$y = $h - 3 * $text_height - 24;
$page_nr = $PAGE_NUM;

$text_left = "FOOTER LEFT HERE";
$width_left = Font_Metrics::get_text_width($text_left, $font,
$size);

$text_center = $page_nr . "/" . $PAGE_COUNT;
$width_center = Font_Metrics::get_text_width("100/100", $font,
$size);

$text_right = "FOOTER RIGHT HERE";
$width_right = Font_Metrics::get_text_width($text_right, $font,
$size);

if ($PAGE_NUM > 1)
{
// FOOTER
$pdf->line(60, $y-3, $w - 60, $y-3, $color, 0.5);
$pdf->text(60, $y, $text_left, $font, $size, $color);
$pdf->text($w / 2 - $width_center / 2, $y, $text_center, $font,
$size, $color);
$pdf->text($w - $width_right - 60, $y, $text_right, $font, $size,
$color);
}
else
{
// JUST TO PROVE IT WRITES SOMETHING ELSE ONLY ON THE FIRST PAGE
$color = array(0,0,0); // nero
$pdf->text(400, 410, $PAGE_COUNT." Pages", $font, $size, $color);
}
';
$pdf->page_script($php_code);
}
</script> <center>
<h1>A BIG TITLE</h1>
<h2>SUBTITLE</h2>
<h2> </h2>
</center>
<p><br /><br /><br /></p>
<table style="width: 100%; page-break-after: always;">
<tbody>
<tr>
<td width="33%">Bla</td>
<td width="34%">Bla</td>
<td width="33%">Bla</td>
</tr>
<tr>
<td><br /><strong>some text</strong><br /><br /></td>
<td><br /><strong>some text</strong><br /><br /></td>
<td><br /><br /><br /></td>
</tr>
<tr>
<td>some text</td>
<td>some text</td>
<td>some text</td>
</tr>
<tr>
<td><br /><strong>some text</strong><br /><br /></td>
<td><br /><strong></strong><br /><br /></td>
<td><br /><strong>some text</strong><br /><br /></td>
</tr>
<tr>
<td>Date:05/05/2011</td>
<td>Date:</td>
<td>Date:</td>
</tr>
</tbody>
</table>
<table style="width: 100%; page-break-after: always;">
<tbody>
<tr>
<td colspan="5"><strong>A TABLE</strong></td>
</tr>
<tr>
<td><strong>A</strong></td>
<td><strong>B</strong></td>
<td><strong>C</strong></td>
<td><strong>D</strong></td>
<td><strong>E</strong></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</tbody>
</table>
<ul style="page-break-after: always;">
<li>1.</li>
<li>2. </li>
<li>3. </li>
<li>4.</li>
<li>5. </li>
</ul>
<h2>Another list List</h2>
<ul>
<li>Bla Bla</li>
<li>Bla Bla</li>
</ul>
<p><br /><br /><br /></p>
<h2>Illustration Index</h2>
<ul style="page-break-after: always;">
<li>Illustration 1: dddd</li>
<li>Illustration 2: eeee</li>
</ul>
<h2>1. INTRODUCTION</h2>
<p style="text-align: justify;">Some very long text here Some very
long text hereSome very long text hereSome very long text hereSome
very long text hereSome very long text hereSome very long text
hereSome very long text hereSome very long text hereSome very long
text hereSome very long text hereSome very long text hereSome very
long text hereSome very long text hereSome very long text hereSome
very long text hereSome very long text hereSome very long text
hereSome very long text hereSome very long text hereSome very long
text hereSome very long text hereSome very long text hereSome very
long text hereSome very long text hereSome very long text hereSome
very long text hereSome very long text hereSome very long text
hereSome very long text hereSome very long text hereSome very long
text hereSome very long text hereSome very long text hereSome very
long text hereSome very long text hereSome very long text hereSome
very long text hereSome very long text here</p>
<h3>1.1 Sub Intro</h3>
<p style="text-align: justify;"><strong>Bla Bla</strong></p>
</body>
</html>

Ariano Lattanzi

unread,
May 5, 2011, 11:32:01 AM5/5/11
to dompdf
Please find also attached the output pdf generated

Help Me!!!! :-(
test.pdf

Martin Thomas Schrott

unread,
May 5, 2011, 11:56:26 AM5/5/11
to dom...@googlegroups.com
Hi,

no idea, I am sorry. I hope that it works on our server, as I never had pdf
files with more than two pages ... will have to check this too, as your
result only worked for two pages. Hope I will have the time to check it out
soon.

If anybody has some more informations or a solution, I am also very happy
;-)

Fabien Ménager

unread,
May 5, 2011, 12:09:23 PM5/5/11
to dom...@googlegroups.com
Hello, I know where the problem is, I'll fix it tonight.

AL

unread,
May 5, 2011, 12:18:18 PM5/5/11
to dompdf
Really?????

I look forward to hearing from you soon!

Thanks guys!

Fabien Ménager

unread,
May 5, 2011, 12:23:52 PM5/5/11
to dom...@googlegroups.com
In fact I got time for dompdf, and I fixed it, can you confirm by updating your local SVN checkout ?

AL

unread,
May 5, 2011, 12:49:17 PM5/5/11
to dompdf
IT WORKS!!!!

Great job, Fabien!

THANK YOU SO MUCH!

Just for the seek of curiosity: what was the problem?

Fabien Ménager

unread,
May 5, 2011, 2:23:27 PM5/5/11
to dom...@googlegroups.com
Great !
I changed something recently that resets the bottom_margin of the first page body, and the top margin of others. These margins are nullified when frames are split accross pages (the body is one of these frames). But the body is the only frame on which we must not do this.

Martin Thomas Schrott

unread,
May 5, 2011, 2:57:35 PM5/5/11
to dom...@googlegroups.com
Fabien!!!
 
If I did understand you correctly, you should emedeately change back, that the margin-bottom of the body is reset to 0 on the first page, if there is more than one page...
 
This would cause footers, inserted via the positioning in html,  to not work anymore on the first page, because the footer will be overlayed by the other content. Wouldn't it?
 
e.g. I currently have headers and footers on all pages including the first one and therefore have to set the top and bottom margin of the body to xyz. So if you reset that, my footers would be lost.
 
Or am I missing something?
 
thanks!
Martin
 
--

Fabien Ménager

unread,
May 5, 2011, 3:41:43 PM5/5/11
to dom...@googlegroups.com
No, I added this margin reset a few days ago, but now this will only affect everything but the body thanks to the fix I made a few hours ago.
I think eveything is OK because the footers inserted via position:fixed are not affected.

Andrei Mlendea

unread,
Oct 26, 2013, 10:42:20 AM10/26/13
to dom...@googlegroups.com
Hi there,

I saw the changes you made working just fine, but still I have a problem with top margin. If I  use no background image or color to the page it looks great but if I use background the top margin is added to the container body starting with the second page an on. I attached  2 files, the html source and  pdf generated. If you can point me to the right direction with this issue I will be more than happy! Thanks

Andy
PDF_invoice-6.pdf
html.rar
Reply all
Reply to author
Forward
0 new messages