在 PHP 中使用 DOMPDF 的 PDF 页面中的标题

发布于 2024-12-05 18:36:51 字数 109 浏览 0 评论 0原文

我正在使用 DOMPDF 创建 PDF 文件。我有大量内容要提取为 PDF,我们需要在所有页面中添加一些标题。那么谁能告诉我如何在 PDF 中添加页眉和页脚,以便使用 DOMPDF 在所有页面中显示页眉。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

把梦留给海 2024-12-12 18:36:51

在 0.6.0 代码中,您将能够使用 HTML+CSS 生成页眉和页脚。与使用内联 PHP 相比,有一些限制(例如,还没有 PAGE_COUNT 占位符),因此这是否可行取决于您的需求。

以下代码将生成一个带有页眉和页脚的两页文档

<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 内容之上。

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.

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.

孤寂小茶 2024-12-12 18:36:51

DOMPDF wiki 上有一个常见问题解答条目:有办法吗添加页眉和页脚或页码?

因此,您可以将以下“内联 PHP”片段添加到您的 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 渲染器,允许您调用 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));

最终您必须在 load_html 之后调用 page_text (只需尝试一下)。

There is a FAQ entry on the DOMPDF wiki: Is there a way to add headers and footers or page numbers?.

So you can either add the following "inline PHP"-snippet to your HTML-input (add a similar page_text-call for your footer):

<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:

// 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).

弥枳 2024-12-12 18:36:51

如果你想得到类似的东西:
输入图像描述此处

定义文档的页眉、页脚和内容如下:

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

If you want to get something similar to:
enter image description here

Define as follows, header, footer and content of your document:

<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>
丶视觉 2024-12-12 18:36:51

您需要此代码,希望对您

有所帮助...

# 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);

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);
梦境 2024-12-12 18:36:51

才将 Blade 与类似的内容一起使用

<!doctype html>
<html lang="en">
<head>
        <style>
header { position: absolute; top: -70px; left: 0px; right: 0px; height: 150px; margin-bottom: 10em }
        </style>
</head>
<body>
       <header>
            <table border="0" width="100%" style="font-size:1em" class="tb-header">
                <tr>
                    <td width="83%" class="left-align" >
                        <tr>
                            <td>
                                <span class="invoice-number mr-1" style="font-size:1em">Purchase Invoice # {{ $data->code }}</span>
                            </td>
                        </tr>
                        <tr>
                            <td style="margin-top: -2px;">
                                <small style="font-size:1em">Diajukan: {{ date('d/m/y',strtotime($data->post_date)) }}</small>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <h5 style="margin-top: -2px">Purchase Invoice</h5>
                            </td>
                        </tr>
                                
                        
                    </td>
                    <td width="33%" class="right-align">
                        
                        
                   
                    </td>
                    
                    <td width="34%" class="right-align">
                        
                            <img src="{{ $image }}" width="50%" style="position: absolute; top:5px; width:20%">
                       
                    </td>
                </tr>
                
            </table>
            <hr style="border-top: 3px solid black; margin-top:-2%">
        </header>
<main>
</main>
</body>

仅在每个 pdf 中呈现此标头后,
请记住!标签标头必须位于主体标签主之前

use blade only with something like this

<!doctype html>
<html lang="en">
<head>
        <style>
header { position: absolute; top: -70px; left: 0px; right: 0px; height: 150px; margin-bottom: 10em }
        </style>
</head>
<body>
       <header>
            <table border="0" width="100%" style="font-size:1em" class="tb-header">
                <tr>
                    <td width="83%" class="left-align" >
                        <tr>
                            <td>
                                <span class="invoice-number mr-1" style="font-size:1em">Purchase Invoice # {{ $data->code }}</span>
                            </td>
                        </tr>
                        <tr>
                            <td style="margin-top: -2px;">
                                <small style="font-size:1em">Diajukan: {{ date('d/m/y',strtotime($data->post_date)) }}</small>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <h5 style="margin-top: -2px">Purchase Invoice</h5>
                            </td>
                        </tr>
                                
                        
                    </td>
                    <td width="33%" class="right-align">
                        
                        
                   
                    </td>
                    
                    <td width="34%" class="right-align">
                        
                            <img src="{{ $image }}" width="50%" style="position: absolute; top:5px; width:20%">
                       
                    </td>
                </tr>
                
            </table>
            <hr style="border-top: 3px solid black; margin-top:-2%">
        </header>
<main>
</main>
</body>

after this header will be rendered in each pdf
Remember!tag header must be on the body before the tag main

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文