在 PHP 中使用 DOMPDF 在 PDF 页面中的页眉
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7484318/
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
Header in PDF page using DOMPDF in PHP
提问by Sandy
I am creating a PDF file using DOMPDF. I have a big content to extract in PDF, we need some header in all the pages. So can anyone telme how to add a header and footer in the PDF so that the header will shown in all the pages using DOMPDF.
我正在使用 DOMPDF 创建一个 PDF 文件。我有大量的 PDF 内容要提取,我们需要在所有页面中添加标题。任何人都可以告诉我如何在 PDF 中添加页眉和页脚,以便使用 DOMPDF 将页眉显示在所有页面中。
回答by BrianS
In the 0.6.0 code you will be able to use HTML+CSS to generate headers and footers. There are a few limitations when compared to using inline PHP (e.g. no PAGE_COUNT placeholder yet), so whether or not this is viable depends on your needs.
在 0.6.0 代码中,您将能够使用 HTML+CSS 生成页眉和页脚。与使用内联 PHP 相比,有一些限制(例如,还没有 PAGE_COUNT 占位符),因此这是否可行取决于您的需求。
The following code will produce a 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>
You could also use a combination of the two styles if you needed access to some of the missing functionality. PDF objects and text added using the page_text
method render on top of the HTML content.
如果您需要访问某些缺失的功能,您也可以结合使用这两种样式。使用page_text
渲染方法在 HTML 内容之上添加的 PDF 对象和文本。
回答by vstm
There is a FAQ entry on the DOMPDF wiki: Is there a way to add headers and footers or page numbers?.
DOMPDF wiki上有一个常见问题条目:是否可以添加页眉和页脚或页码?.
So you can either add the following "inline PHP"-snippet to your HTML-input (add a similar page_text
-call for your footer):
因此,您可以将以下“内联 PHP”-snippet 添加到您的 HTML-input(page_text
为您的页脚添加一个类似的-call):
<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>
If you rather want to implement this on your caller-side (meaning in the PHP code directly) you have to call the DOMPDFS's get_canvas()
-method which returns the underlying PDF-Renderer which allows you to call the page_text
method like in the example above. Let me show you what I mean:
如果您希望在调用方(即直接在 PHP 代码中)实现这一点,您必须调用 DOMPDFS 的get_canvas()
-method,它返回底层 PDF-Renderer,它允许您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));
Eventually you have to call page_text
after load_html
(just try it out).
最终,你必须调用page_text
后load_html
(只是尝试一下)。
回答by Enrique Jaime Mu?iz
You need This code, I hope help you
您需要此代码,希望对您有所帮助
Regards...
问候...
# Instanciamos un objeto de la clase DOMPDF.
$dompdf = new DOMPDF();
# Definimos el tama?o y orientación del papel que queremos.
# O por defecto cogerá el que está en el fichero de configuración.
$dompdf->set_paper("Letter", "portrait");
# Cargamos el contenido HTML.
$dompdf->load_html(utf8_decode($html));
# Renderizamos el documento PDF.
$dompdf->render();
#Esto es lo que imprime en el PDF el numero de paginas
$canvas = $dompdf->get_canvas();
$footer = $canvas->open_object();
$w = $canvas->get_width();
$h = $canvas->get_height();
$canvas->page_text($w-60,$h-28,"Página {PAGE_NUM} de {PAGE_COUNT}", Font_Metrics::get_font('helvetica'),6);
$canvas->page_text($w-590,$h-28,"El pie de página del lado izquiero, Guadalajara, Jalisco C.P. XXXXX Tel. XX (XX) XXXX XXXX", Font_Metrics::get_font('helvetica'),6);
$canvas->close_object();
$canvas->add_object($footer,"all");
# Enviamos el fichero PDF al navegador.
//$dompdf->stream('FicheroEjemplo.pdf');
# Para grabar en fichero en ruta especifica
$output = $dompdf->output();
file_put_contents('ejemplo.pdf',$output);
#Liberamos
unset($dompdf);