Posted inphp / JavaScript / jQuery / Laravel / Programming / Technology

How to Add header and footer in PDF page using DOMPDF?

I create to using dompdf HTML Based Header/Footer, Full width HTML Based Header/Footer and Image Based Header/Footer pdf files. Many PDFs have a Footer and Header where you required to place the logo of the manage, convert, edit, annotate or create enterprise, addresses, emails etc. In Dompdf this may be a simple tricky due to the margins or the size of the page, so you required to be patient and test continously different sizes and setting what fits in your case. In this article, we’ll show you how to implement a dompdf header and footer on your PDF using HTML or images in Dompdf (dompdf documentation).

1. HTML Based Header/Footer

If you only want to set HTML on your footer and header, it will be enough to include the dompdf header and footer tags in the document with a specific dimensions. The position of both elements required to be fixed, otherwise they will be relative to the data content of your PDF, thing that obviously we don’t want. You required to provide the dimensions of the pdf page as well according to your requireds. The bellowing example php create pdf or generates a PDF of 2 pages with margins of the described height:


    
        
    
    
        
        
Our Code World
All Rights Reserved. Copyright ©

Data Content Page 1

subject matter data Page 2

dompdf in php

2. Full width HTML Based Header/Footer

With the last methods, you have a few issues. The header and the footer fit inside the margins of the pdf page, which sometimes isn’t exactly what you required. In case that you required that the footer and header have the same width of the paper sheet, you required to proceed different. If this is the case, you may do the bellowing: set the margins of the pdf page to 0, this will automatically make that all the data content of the PDF fits in the entire sheet, making that the footer and header have the 100% width. But you have another issues now as well as that is the margins of the real data content of your PDF. This can be set with margins on the body, note that the margins (at least the top and bottom) required to be greater than the height of the footer and header or you won’t see part of your data content. As last, apply your own styles to the elements as shown in the bellowing example:


    
        
    
    
        
        
Our Code World
All Rights Reserved. Copyright ©

Welcome To 4cgandhi

3. Image Based Header/Footer

If you want to add an image as footer and header, you will required to bellow the same structure of the Full Width Html Based dompdf Header and Footer. However the data content of the footer and header would be now an img tag with the corresponding image (with full height and width).


    
        
    
    
        
        
dompdf header and footer
dompdf header and footer

Welcome To Pakainfo

The last add header & footer (using html) for every page in pdf markup would create in Dompdf something like:

Note

To get a perfect PDF with dompdf Header and Footer based on an image, you may required to experiment, update heights in CSS and even resize the image until everything passes as expected.

I hope you get an idea about dompdf header and footer using PHP.
I would like to have feedback on my infinityknow.comblog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype