Home > Blockchain >  Modify existing pdf document with php
Modify existing pdf document with php

Time:10-12

So what I would like to do is pretty straight forward, I would like to modify an existing pdf document.

Instead of writing to an existing pdf where I added, it writes to a blank file.

Here is the code.

<?php

require('vendor/autoload.php');

$mpdf = new mPDF();

$mpdf->AddPage();
// set the sourcefile
$mpdf->setSourceFile('hs.pdf');
// import page 1
$tplIdx = $mpdf->importPage(1);
// use the imported page and place it at point 10,10 with a width of 200 mm   (This is the image of the included pdf)
$mpdf->useTemplate($tplIdx, 10, 10, 200);
// now write some text above the imported page
$mpdf->SetTextColor(0,0,255);

$mpdf->SetFont('Arial','B',8);
$mpdf->SetXY(95, 16);
$mpdf->Write(0, "Mindfire");
$mpdf->Output('newpdf.pdf');

This is the image I want to write to. enter image description here

Add this is the image it outputs enter image description here

As you can see it seems to be just writing to a blank document every time instead of writing the the first pdf.

Any ideas ?

Update:

Here is my composer.json file

 {
  "require": {
    "mpdf/mpdf": "v5.5.1"
  }
}

Ive tried all different verison of mpdf but the same error persists.

Uncaught Error: Class 'Mpdf\Mpdf' not found in

CodePudding user response:

It seems there has been some confusion surrounding the mPDF version syntax and composer usage.
Since you've tried some ill-advised workarounds, I suggest resetting the composer environment and Reinstalling mPDF.

Set the project directory as your CWD

cd /path/to/project

Delete the composer managed files

Linux OS

rm -rf ./vendor
rm ./composer.json
rm ./composer.lock

Windows OS cmd

rmdir /Q /S .\vendor
del .\composer.json
del .\composer.lock

Windows OS PowerShell

Remove-Item -Recurse -Force .\vendor
Remove-Item .\composer.json
Remove-Item .\composer.lock

Reinstall mPDF library files

composer require mpdf/mpdf

Your project directory should contain the following:
Where pdf_creator.php is the script used to generate the PDF.

project/
   composer.json
   hs.pdf
   pdf_creator.php
   vendor/
      mpdf/
      autoload.php
      ...

Check the version of mPDF in your composer.json file
Depending on the version use one of the examples below.

{
    "require": {
        "mpdf/mpdf": "^8.0"
    }
}

mPDF 4.3 to 6.x

Method names use pascal-cased pattern
No namespace
Classname is mPDF()

Example: example41_MPDFI_template.php

<?php
/* pdf_creator.php */

require_once __DIR__ . '/vendor/autoload.php';

$mpdf = new mPDF();

// set the sourcefile
$mpdf->SetImportUse(); // <--- required for mPDF versions < 8.0
$mpdf->SetSourceFile(__DIR__ . '/hs.pdf'); // absolute path to pdf file

// import page 1
$tplIdx = $mpdf->ImportPage(1);

// use the imported page and place it at point 10,10 with a width of 200 mm   (This is the image of the included pdf)
$mpdf->UseTemplate($tplIdx, 10, 10, 200);

// now write some text above the imported page
$mpdf->SetTextColor(0, 0, 255);
$mpdf->SetFont('Arial', 'B', 8);
$mpdf->SetXY(95, 16);
$mpdf->Write(0, 'Mindfire');
$mpdf->Output('newpdf.pdf');

mPDF 7.x

Method names use pascal-cased pattern
Introduced the \Mpdf namespace
Classname is Mpdf()

Example Importing Files & Templates

<?php
/* pdf_creator.php */

require_once __DIR__ . '/vendor/autoload.php';

$mpdf = new \Mpdf\Mpdf();

// set the sourcefile
$mpdf->SetImportUse(); // <--- required for mPDF versions < 8.0
$mpdf->SetSourceFile(__DIR__ . '/hs.pdf'); // absolute path to pdf file

// import page 1
$tplIdx = $mpdf->ImportPage(1);

// use the imported page and place it at point 10,10 with a width of 200 mm   (This is the image of the included pdf)
$mpdf->UseTemplate($tplIdx, 10, 10, 200);

// now write some text above the imported page
$mpdf->SetTextColor(0, 0, 255);
$mpdf->SetFont('Arial', 'B', 8);
$mpdf->SetXY(95, 16);
$mpdf->Write(0, 'Mindfire');
$mpdf->Output('newpdf.pdf');

mPDF 8.x

Method names use camel-cased pattern
Introduced the \Mpdf namespace
Classname is Mpdf()
Method Mpdf::SetImportUse() was removed

Example Importing Files & Templates

<?php
/* pdf_creator.php */

require_once __DIR__ . '/vendor/autoload.php';

$mpdf = new \Mpdf\Mpdf();

// set the sourcefile
// $mpdf->SetImportUse(); // <--- not needed for mPDF version 8.0 
$mpdf->setSourceFile(__DIR__ . '/hs.pdf'); // absolute path to pdf file

// import page 1
$tplIdx = $mpdf->importPage(1);

// use the imported page and place it at point 10,10 with a width of 200 mm   (This is the image of the included pdf)
$mpdf->useTemplate($tplIdx, 10, 10, 200);

// now write some text above the imported page
$mpdf->SetTextColor(0, 0, 255);
$mpdf->SetFont('Arial', 'B', 8);
$mpdf->SetXY(95, 16);
$mpdf->Write(0, 'Mindfire');
$mpdf->Output('newpdf.pdf');

Now run your script from the CLI to see it emits any errors.

cd /path/to/project
php pdf_creator.php

Note

  • $mpdf->AddPage(); is not needed for editing a PDF file, unless adding another page to the resulting output PDF.
  • Related