Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9210145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:59:59+00:00 2026-06-18T00:59:59+00:00

This looks like the simplest thing but I can’t get it to work. I

  • 0

This looks like the simplest thing but I can’t get it to work.

I need to add text to the first page of a multi-page pdf (could be any number of pages)

Using this code on a two page pdf (without the for loop, just using $pdf->importPage(2)) I end up with two pages but the second page is a repeat of page one. The text is written on the first page only which is good but I need all pages included in the output pdf. Here is my code

// Original file with multiple pages 
$fullPathToFile = 'full/path/to/file.pdf';

class PDF extends FPDI {

    var $_tplIdx;

    function Header() {

        global $fullPathToFile;

        if (is_null($this->_tplIdx)) {

            $this->setSourceFile($fullPathToFile);
            $this->_tplIdx = $this->importPage(1);

        }
        $this->useTemplate($this->_tplIdx);

    }

    function Footer() {}

}

// initiate PDF
$pdf = new PDF();
$pdf->setFontSubsetting(true);


// add a page
$pdf->AddPage();

// The new content
$pdf->SetFont("helvetica", "B", 14);
$pdf->Text(10,10,'Some text here');

// How to get the number of pages of original pdf???
// $numPages = $pdf->getNumPages(???);

// Carry on adding all remaining pages starting from page 2
for($i=2;$i<=$numPages;$i++) {
    // Add another page
    $pdf->AddPage();
    // Do I need to declare the source file here?
    // $pdf->setSourceFile($fullPathToWD);
    $pdf->importPage($i);
}

// Output the file as forced download
$pdf->Output('theNewFile.pdf', 'D');

Links to docs

TCPDF Classes
http://www.tcpdf.org/doc/code/classTCPDF.html#a5171e20b366b74523709d84c349c1ced

FPDI Classes
http://www.setasign.de/support/manuals/fpdi/

FPDF_TPL Classes
http://www.setasign.de/support/manuals/fpdf-tpl/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:00:00+00:00Added an answer on June 18, 2026 at 1:00 am

    Solved my problem…

    // Original file with multiple pages 
    $fullPathToFile = 'full/path/to/file.pdf';
    
    class PDF extends FPDI {
    
        var $_tplIdx;
    
        function Header() {
    
            global $fullPathToFile;
    
            if (is_null($this->_tplIdx)) {
    
                // THIS IS WHERE YOU GET THE NUMBER OF PAGES
                $this->numPages = $this->setSourceFile($fullPathToFile);
                $this->_tplIdx = $this->importPage(1);
    
            }
            $this->useTemplate($this->_tplIdx);
    
        }
    
        function Footer() {}
    
    }
    
    // initiate PDF
    $pdf = new PDF();
    $pdf->setFontSubsetting(true);
    
    
    // add a page
    $pdf->AddPage();
    
    // The new content
    $pdf->SetFont("helvetica", "B", 14);
    $pdf->Text(10,10,'Some text here');
    
    // THIS PUTS THE REMAINDER OF THE PAGES IN
    if($pdf->numPages>1) {
        for($i=2;$i<=$pdf->numPages;$i++) {
            $pdf->endPage();
            $pdf->_tplIdx = $pdf->importPage($i);
            $pdf->AddPage();
        }
    }
    
    // Output the file as forced download
    $pdf->Output('theNewFile.pdf', 'D');
    

    You get the number of pages by adding the first part of this line

    $this->numPages = $this->setSourceFile($fullPathToFile);
    

    And see the second last block of code – the for loop adds the remainder of the pages.

    Don’t know if this is how it should be done? I read in a few places that it wasn’t even possible to achieve this, also the code is not supplied in the docs. However, this works, hope it helps someone.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This looks like a long shot, but does anyone know of a way to:
I found this script attached to a modified index page. This looks like some
I'm trying to make the so called fine tune thing. Basically this looks like:
It looks like this question has been asked dozens of times, but none of
This seems like the simplest thing in the world and I'm ready to pull
I can't even get the simplest kind of binding library to work - I
I want to know what this looks like. I don't have any ideas about
I'm playing with one stack overflow example. This example looks like this: void return_input
It looks like this block directly flushes the output. What's the practical use of
jquery looks like this $.post('JSP/processForm.jsp', $(#Form).serialize(), function(data){ //I want to iterate through every line

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.