我正在使用DOMPDF创建PDF文件。我有一个很大的内容要提取PDF格式,我们需要在所有页面中都有一些标题。因此,任何人都可以在PDF中添加页眉和页脚,以便使用DOMPDF在所有页面中显示页眉。
答案 0 :(得分:55)
在0.6.0代码中,您将能够使用HTML + CSS生成页眉和页脚。与使用内联PHP相比,存在一些限制(例如,还没有PAGE_COUNT占位符),因此这是否可行取决于您的需求。
以下代码将生成two-page document with a header and footer:
<html>
<head>
<style>
@page { margin: 180px 50px; }
#header { position: fixed; left: 0px; top: -180px; right: 0px; height: 150px; background-color: orange; text-align: center; }
#footer { position: fixed; left: 0px; bottom: -180px; right: 0px; height: 150px; background-color: lightblue; }
#footer .page:after { content: counter(page, upper-roman); }
</style>
<body>
<div id="header">
<h1>Widgets Express</h1>
</div>
<div id="footer">
<p class="page">Page </p>
</div>
<div id="content">
<p>the first page</p>
<p style="page-break-before: always;">the second page</p>
</div>
</body>
</html>
如果您需要访问某些缺少的功能,也可以使用这两种样式的组合。使用page_text
方法添加的PDF对象和文本在HTML内容之上呈现。
答案 1 :(得分:19)
DOMPDF wiki上有一个FAQ条目:有没有办法添加页眉和页脚?。
因此,您可以将以下“内联PHP”-snippet添加到HTML输入中(为您的页脚添加类似的page_text
- 调用):
<script type="text/php">
if ( isset($pdf) ) {
$font = Font_Metrics::get_font("helvetica", "bold");
$pdf->page_text(72, 18, "Header: {PAGE_NUM} of {PAGE_COUNT}",
$font, 6, array(0,0,0));
}
</script>
如果你想在你的调用者端实现这个(直接在PHP代码中),你必须调用DOMPDFS的get_canvas()
- 方法,它返回底层的PDF-Renderer,它允许你调用{ {1}}方法,如上例所示。让我告诉你我的意思:
page_text
最后,您必须在// your dompdf setup
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
// add the header
$canvas = $dompdf->get_canvas();
$font = Font_Metrics::get_font("helvetica", "bold");
// the same call as in my previous example
$canvas->page_text(72, 18, "Header: {PAGE_NUM} of {PAGE_COUNT}",
$font, 6, array(0,0,0));
之后致电page_text
(只是尝试一下)。
答案 2 :(得分:0)
如下定义文档的页眉、页脚和内容:
<html>
<head>
<style>
@page {
margin: 100px 25px;
}
header {
position: fixed;
top: -60px;
height: 50px;
background-color: #752727;
color: white;
text-align: center;
line-height: 35px;
}
footer {
position: fixed;
bottom: -60px;
height: 50px;
background-color: #752727;
color: white;
text-align: center;
line-height: 35px;
}
</style>
</head>
<body>
<header>
Header
</header>
<footer>
Footer
</footer>
<main>
<!-- Your main content -->
</main>
</body>
</html>
答案 3 :(得分:-1)
您需要此代码,希望对您有所帮助
关于...
ssh -R youruniquesubdomain:80:localhost:3000 serveo.net