I’m trying to add a page with an annotation box to an existing signed pdf. This should be possible with TCPDF and FPDF. Unfortunately, it removes the sign and the pdf cannot be validated after being altered. The displayed pdf looks the same, though.
The code (generated by AI) follows:
<?php
use FpdfFpdf;
use setasignFpdiFpdi;
require_once '../lib/fpdf/Fpdf.php';
require_once '../lib/tcpdf/tcpdf.php';
require_once '../lib/fpdi/src/autoload.php';
// require_once '../vendor/autoload.php';
$pdf = new Fpdi();
$pdf->setSourceFile('test-form-signed.pdf');
// Loop through the pages of the original PDF
$pageCount = $pdf->setSourceFile('test-form-signed.pdf');
for ($i = 1; $i <= $pageCount; $i++) {
$tplId = $pdf->importPage($i);
$pdf->addPage();
$pdf->useTemplate($tplId, 0, 0);
}
// Add a blank new page at the end
$pdf->AddPage();
// Set annotation properties
$pdf->SetFont('Helvetica', '', 12);
$pdf->SetTextColor(0, 0, 0);
$pdf->Text(10, 10, "This is a text annotation on a new blank page.");
// Save the output PDF
$pdf->Output('F', 'test-form-annotated-signed.pdf');
The test-form-annotated-signed.pdf is about half the size of the original file and it no longer passes a validation test. Probably, the signature/certificate was removed during the process.
By the way, the pdf is signed with a OCES-3 certificate issued by the State of Denmark.
Please note that solutions that involve using Adobe Acrobat, Signicat etc. are not an option.
2
Answers
This can be achieved by using the paid software package, Setasign-Core. The following demonstrates it (code found in the link provided by @JanSlabom in the comments):
You cannot modify an existing PDF with FPDI.
With FPDI you import existing pages into a reusable structure (XObjects) which can be used with e.g. FPDF or TCPDF.
The resulting PDF is a completely new PDF with a completely different internal structure.
Most PDF viewers allow you to annotate signed PDFs (via incremental updates) but will note these modifications.
FWIK: It is impossible to add an additional page without invalidating existing the signatures.