Add Header to PDF using PHP

Add Header to PDF File using PHP.

Add Headers to PDF Document Using PHP

In order to add Header in PDF file, we’ll use Aspose.PDF for PHP via Java API which is a feature-rich, powerful and easy-to-use document manipulation tool in php-java. Install Tomcat 9.0 version on any location, add Aspose.PDF.war, for more details check the GitHub page.

Add Header to PDF with PHP


To try the code in your environment, you need Aspose.PDF for PHP via Java.

  1. Open a PDF document using Document object.
  2. Create a Stamp and define its properties.
  3. Add the Stamp to Page using AddStamp method.
  4. Save the PDF file.

Add a Header to PDF Document - PHP

This sample code shows how to add Header to PDF

    // Open document
    $document = new Document($inputFile);
    
    // Create footer
    $imageStamp = new ImageStamp($inputImage);
    $horizontalAlignment = new HorizontalAlignment();
    $verticalAlignment = new VerticalAlignment();

    // Set properties of the stamp
    $imageStamp->setTopMargin(10);
    $imageStamp->setHorizontalAlignment($horizontalAlignment->Center);
    $imageStamp->setVerticalAlignment($verticalAlignment->Top);

    $pages = $document->getPages();

    // Add footer to 1st page
    $page = $pages->get_Item(1);
    $page->addStamp($imageStamp);

    // Save output document
    $document->save($outputFile);
    $document->close();