Header in PDF page using DOMPDF in PHP

前端 未结 3 2020
终归单人心
终归单人心 2020-11-27 02:26

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 th

3条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 03:30

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

        
    

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

提交回复
热议问题