0

I tried found solution in https://mpdf.github.io/reference/mpdf-functions/writetext.html but I am not able to found proper solution.

Actually source pdf to add/write one line in first pdf page but it always return 1 pdf page in output, Rest of pages are gone. Really strange issue. Actually my PDF have multiple page.

Here is my code.

require_once APPPATH . './libraries/mpdf/vendor/autoload.php';
if (ob_get_contents()) ob_end_clean();
$Text = "This score is for Mr. John Deo";
$mpdf = new \Mpdf\Mpdf();
$mpdf->SetImportUse(); // only with mPDF <8.0
$file = Sample.pdf'; //This PDF have 2 pages
$pagecount = $mpdf->SetSourceFile($file);
$tplId = $mpdf->ImportPage($pagecount);
$mpdf->UseTemplate($tplId);
$mpdf->SetFont('Arial','B', 11);
$mpdf->WriteText(10,10, $Text );
$mpdf->Output('new.pdf', 'F'); //Return 1 page in output with writetext.

Can you please let me know what is wrong with this code? This code is only return first page not all pages in output.

Kuppusamy
  • 453
  • 3
  • 11
Vipul Jethva
  • 647
  • 1
  • 7
  • 27

1 Answers1

0

The ImportPage function import only one page (see the doc here).

You could use a loop to import each page of your source document.

(edited to add sample code)

Sample code :

  • Meant to be added before the call to Output() method.
  • You have to replace $tplId = $mpdf->ImportPage($pagecount); by $tplId = $mpdf->ImportPage(1); to write on the first page as SetSourceFile() method return the total number of page in the source document.
  • the loop start at the second page, because your code already import and write to first page (see prior point).
for ($i = 2; $i <= $pagecount; $i++)
{
    $mpdf->AddPage(); // Add page to output document
    $tplId = $mpdf->ImportPage($i); // Import page as a template
    $mpdf->UseTemplate($tplId); // insert the template in the added page
}

(The sample is inspired by this answer.)

Please note that it's just a sample code, you should check if the source file exists, check the total number of pages before running the loop, etc.