php dompdf HTML to PDF - 无法设置页面边距
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19779285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dompdf HTML to PDF - can't set margin of page
提问by Nereo Costacurta
version: 0.6.0 beta 3
版本:0.6.0 测试版 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...
我以各种方式尝试让 dompdf 设置页面的边距。我有一个很长的页面,里面有很多文字,分成几章……
my css is something like:
我的 css 是这样的:
#wrapper{
padding:8px;
}
/* other styles... */
@page{margin: 0.2in 0.5in 0.2in 0.5in;}
when the php is
当 php 是
<?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...
但我得到的是一个没有边距的页面!!!仅应用#wrapper 的填充...并且它们仅应用在整个PDF 的开头和结尾...
I'm doing something wrong?
我做错了什么?
PS - it seems that only the bottom-margin is applied... but I'm not sure...
PS - 似乎只应用了底边距......但我不确定......
PPS - I tryed with no success also this css: body { margin-top: 40px; } html { margin-top: 40px; } @page { margin-top: 40px; }
PPS - 我也尝试过这个 css: body { margin-top: 40px; } html {边距顶部:40px; } @page { margin-top: 40px; }
回答by Nereo Costacurta
I figured out that neither body or @page works in this version of dompdf.
我发现 body 或 @page 在这个版本的 dompdf 中都不起作用。
the problem was in the main CSS, where I put every tyme this line:
问题出在主要的 CSS 中,我把每一行都放在这里:
*{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:
我发现 PDF 的边距是根据 HTML 的边距决定的,所以我用全局选择器删除了该行并替换为:
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).
现在我很高兴。我真的不知道谁反对我的问题,我认为这非常重要!事实上,互联网上到处都有错误的答案(使用正文或@page margin)。
solution: use in CSS html{margin:...}
解决方案:在 CSS html{margin:...} 中使用
回答by servetoz32
This fixed my problem:
这解决了我的问题:
@page {
margin: 0px 0px 0px 0px !important;
padding: 0px 0px 0px 0px !important;
}