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

owfi6suc  于 2023-05-27  发布在  PHP
关注(0)|答案(5)|浏览(135)

我正在使用DOMPDF创建一个PDF文件。我有一个大的内容提取PDF,我们需要在所有的网页一些头。因此,任何人都可以告诉我如何添加一个页眉和页脚在PDF中,使页眉将显示在所有的页面使用DOMPDF。

e5nszbig

e5nszbig1#

在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内容的顶部。

whlutmcx

whlutmcx2#

DOMPDF wiki上有一个FAQ条目:* 是否有方法添加页眉和页脚或页码?*.
因此,您可以将以下“inline 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>

如果你想在你的调用端实现它(直接在PHP代码中),你必须调用DOMPDFS的get_canvas()-方法,它返回底层的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));

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

ztyzrc3y

ztyzrc3y3#

如果你想得到类似的东西:

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

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

bq8i3lrv4#

你需要这个代码,希望对你有帮助
问候...

# 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&aacute;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);
daolsyd0

daolsyd05#

用刀片只与这样的东西

<!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中呈现标题
记住!标记头必须在主体上,在标记main之前

相关问题