Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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; }

like image 784
Nereo Costacurta Avatar asked Nov 04 '13 23:11

Nereo Costacurta


People also ask

How do I change the paper size in Dompdf?

module, domPDF is hardcoded to use a4 paper size, and portrait for the orientation: $html = theme('commerce_invoice_pdf_page', ['invoice' => $invoice]); $dompdf->set_paper('letter', 'portrait'); $dompdf->load_html($html); $dompdf->render(); I feel we should pull the full list of sizes from CPDF, which domPDF relies on.

How do you insert a page break in Dompdf?

Using page-break-inside: auto; basically says to dompdf "do what you would normally do when breaking pages." To force a page break before/after your table you would use page-break-before: always; / page-break-after: always; . To ask dompdf to avoid breaking inside an element you would use page-break-inside: avoid; .

What is Dompdf library?

Dompdf is an HTML to PDF converter It is a style-driven renderer: it will download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements. It also supports most presentational HTML attributes.

How do I add a footer in Dompdf?

Just like the header, we will put our footer in a footer tag inside the body tag of the template. Just like header, we will create a space for our footer on every page then use part of the space for our footer. For the body content, put everything in a main tag inside the body tag of the template.


1 Answers

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.

NOW I'M HAPPY. I really don't know who down-vote my question, I think it's very important! In fact everywhere in internet there's the wrong answer (using body or @page margin).

solution: use in CSS html{margin:...}

like image 61
Nereo Costacurta Avatar answered Sep 16 '22 12:09

Nereo Costacurta