Edit PDF en PHP?
If you are taking a 'fill in the blank' approach, you can precisely position text anywhere you want on the page. So it's relatively easy (if not a bit tedious) to add the missing text to the document. For example with Zend Framework:
<?php
require_once 'Zend/Pdf.php';
$pdf = Zend_Pdf::load('blank.pdf');
$page = $pdf->pages[0];
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
$page->setFont($font, 12);
$page->drawText('Hello world!', 72, 720);
$pdf->save('zend.pdf');
If you're trying to replace inline content, such as a "[placeholder string]," it gets much more complicated. While it's technically possible to do, you're likely to mess up the layout of the page.
A PDF document is comprised of a set of primitive drawing operations: line here, image here, text chunk there, etc. It does not contain any information about the layout intent of those primitives.
Modify existing pdf document with php
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.
Writing/Drawing over a PDF template document in PHP
Have a look at the FPDI Library an add on to FPDF for template annotation.
It can also bolt-on to TCPDF, another popular PHP PDF library. An existing PDF is used as the base of a page, instead of a blank, after that the procedures are the same as regular PDF creation.
Can't edit a existing PDF file with FPDI
A. FPDI download
After you download the fpdi from say https://github.com/Setasign/FPDI, please use the following to get the fpdi started:
require_once 'FPDI-master/src/autoload.php';
require_once('FPDI-master/src/fpdi.php');
The following is a fully working example which I have used in the past for your reference (I've used TCPDF, but I have changed to use fpdf):
B. Testing PHP: testgen.php
<?php
require_once 'vendor/autoload.php';
//require_once('tcpdf/tcpdf.php');
require_once('fpdf/fpdf.php');
require_once('vendor/setasign/fpdi/fpdi.php');
$pdf = new FPDI();
$pagecount = $pdf->setSourceFile('ok.pdf');
for ($n = 1; $n <= $pagecount; $n++) {
$pdf->AddPage();
$tplIdx = $pdf->importPage($n);
$pdf->useTemplate($tplIdx);
$pdf->SetFont('Helvetica', 'B', 10);
$pdf->SetXY(150, 10);
$pdf->Write(0, "Appendix 1(new)");
}
$pdf->Output("output_sample_ken.pdf", "D");
?>
In order to faciliate you to further test it, you may download the fpdf / fpdi files from this link:
http://www.createchhk.com/SO/pdfpack_20June2021.zip
after that, unzip and upload the files to your webserver PHP folder, then use a browser to run the testgen.php to see the effect. (the php will add the text Appendix 1(new)
on each page of the original ok.pdf
file, and then download the file)
C. Problem in processing Encrypted PDF
Last but not least, please note that FPDI does not support importing of encrypted PDF documents see the following link:
https://www.setasign.com/support/faq/fpdi/can-fpdi-import-encrypted-pdf-documents/
In my experience, to process an encrypted pdf, you may use something like pdf995 to "print" the encrypted pdf so as to generate a normal pdf, then this latter pdf can be processed by FPDI.
Related Topics
Laravel: Syntax Error or Access Violation: 1055 Error
How to Find Entry by Object Property from an Array of Objects
Running MySQL *.SQL Files in PHP
How to Run a .Bat File from PHP
Fatal Error: Call to Undefined Function MySQL_Connect()
Preserving Session Variables Across Different Domains
Strict Standards: MySQLi_Next_Result() Error With MySQLi_Multi_Query
Print Numeric Values to Two Decimal Places
PHPdoc Type Hinting For Array of Objects
How to Post Pictures to Instagram Using API
How Do PHP Sessions Work? (Not "How Are They Used")
How to Test a Url For 404 in PHP
Remove Excess Whitespace from Within a String
How to Increase Maximum Execution Time in PHP
Pdo::_Construct(): Server Sent Charset (255) Unknown to the Client. Please, Report to the Developers