Adding Digital Signatures and Watermarks to PDFs in PHP

Adding Digital Signatures and Watermarks to PDFs in PHP

Introduction

In professional applications like contracts, invoices, legal documents, and confidential reports, ensuring authenticity and preventing unauthorized modifications is crucial.

PHP allows adding digital signatures and watermarks to PDFs, making them tamper-proof and branded.

In this guide, we’ll cover:

Adding a digital signature to a PDF using TCPDF
Applying a text and image watermark to a PDF
Securing PDFs to prevent unauthorized editing
Verifying a signed PDF document

By the end, you'll have a PHP-based solution to add security, branding, and authenticity to PDFs. 🚀

1. Installing Required Libraries (TCPDF, FPDI, and FPDF)

Install via Composer (Recommended)

composer require tecnickcom/tcpdf
composer require setasign/fpdf
composer require setasign/fpdi

Include them in your PHP script:

require 'vendor/autoload.php';

use setasign\Fpdi\Fpdi;
use setasign\Fpdi\PdfReader;
use TCPDF;

Now, your system is ready for PDF security enhancements!

2. Adding a Digital Signature to a PDF in PHP (TCPDF Method)

A digital signature ensures that a PDF hasn’t been modified and authenticates the document’s source.

Example: Sign a PDF with a Digital Signature

require 'vendor/autoload.php';

use TCPDF;

// Create PDF
$pdf = new TCPDF();
$pdf->SetCreator('My Company');
$pdf->SetAuthor('John Doe');
$pdf->SetTitle('Signed Document');
$pdf->AddPage();
$pdf->SetFont('helvetica', '', 12);
$pdf->Cell(0, 10, 'This document is digitally signed!', 0, 1, 'C');

// Digital Signature
$certificate = 'file://path/to/certificate.pem';
$pdf->setSignature($certificate, $certificate, 'mypassword', '', 2, ['Name' => 'John Doe', 'Reason' => 'Verified Document']);

// Output signed PDF
$pdf->Output('signed_document.pdf', 'F');

echo "PDF signed successfully!";

Explanation:

setSignature() – Applies a digital signature to the PDF.
Uses a .pem certificate (must be generated beforehand).
Prevents unauthorized modifications.

🔹 Certificates can be generated using OpenSSL:

openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365

3. Adding a Text Watermark to a PDF (FPDI & FPDF Method)

A text watermark can be used for branding or marking documents as confidential.

Example: Adding a Text Watermark

class WatermarkedPDF extends Fpdi {
    function Header() {
        $this->SetFont('Arial', 'B', 50);
        $this->SetTextColor(200, 200, 200);
        $this->Rotate(45, 60, 60);
        $this->Text(30, 210, 'CONFIDENTIAL');
        $this->Rotate(0);
    }
}

// Apply Watermark
$pdf = new WatermarkedPDF();
$pdf->setSourceFile('original.pdf');
$pageCount = $pdf->setSourceFile('original.pdf');

for ($i = 1; $i <= $pageCount; $i++) {
    $pdf->AddPage();
    $tplIdx = $pdf->importPage($i);
    $pdf->useTemplate($tplIdx);
}

$pdf->Output('watermarked.pdf', 'F');
echo "Watermark added successfully!";

Explanation:

Overlays a semi-transparent "CONFIDENTIAL" watermark on each page.
Uses Rotate() for angled text placement.

4. Adding an Image Watermark (Logo or Stamp)

Image watermarks are ideal for company branding, approval stamps, or official seals.

Example: Adding a Logo Watermark to a PDF

class ImageWatermarkedPDF extends Fpdi {
    function Header() {
        $this->Image('logo.png', 50, 100, 100, 100, 'PNG');
    }
}

$pdf = new ImageWatermarkedPDF();
$pdf->setSourceFile('original.pdf');
$pageCount = $pdf->setSourceFile('original.pdf');

for ($i = 1; $i <= $pageCount; $i++) {
    $pdf->AddPage();
    $tplIdx = $pdf->importPage($i);
    $pdf->useTemplate($tplIdx);
}

$pdf->Output('image_watermarked.pdf', 'F');
echo "Image watermark added successfully!";

Explanation:

Embeds a watermark image (e.g., company logo or stamp).
Uses Image('logo.png', x, y, width, height, 'PNG').

🔹 Change coordinates to adjust placement.

5. Protecting PDFs from Unauthorized Editing

To prevent unauthorized modifications, restrict editing or copying permissions.

Example: Secure a PDF Against Editing & Copying

$pdf = new TCPDF();
$pdf->SetProtection(['modify', 'copy'], 'userpassword', 'ownerpassword');
$pdf->AddPage();
$pdf->SetFont('helvetica', '', 12);
$pdf->Cell(0, 10, 'This PDF is protected!', 0, 1, 'C');
$pdf->Output('protected.pdf', 'F');

echo "PDF secured successfully!";

Explanation:

SetProtection() – Blocks editing and copying.
User can view but not modify the document.

6. Verifying a Digitally Signed PDF

After signing a document, verification is required to ensure it hasn’t been altered.

Verify Signature Using Adobe Acrobat:

  1. Open the signed PDF in Adobe Acrobat Reader.
  2. Click on "Signatures" in the left panel.
  3. Adobe will show if the signature is valid or the document has been modified.

Ensures document authenticity and security.

7. Automating Signature and Watermarking for Uploaded PDFs

To automate signing and watermarking upon file upload, modify the upload script.

Example: Auto-Sign and Watermark PDF Upon Upload

if ($_FILES['pdf']['error'] == 0) {
    $uploadDir = "uploads/";
    $pdfPath = $uploadDir . basename($_FILES['pdf']['name']);

    if (move_uploaded_file($_FILES['pdf']['tmp_name'], $pdfPath)) {
        // Add watermark or signature
        addTextWatermark($pdfPath, "watermarked.pdf");
        echo "PDF uploaded and processed!";
    } else {
        echo "Upload failed!";
    }
}

Automatically adds watermark/signature when a user uploads a PDF.

Best Practices for PDF Security in PHP

Use digital signatures for legal documents to ensure authenticity.
Apply watermarks to prevent unauthorized duplication.
Restrict editing/copying for sensitive PDFs.
Verify signed PDFs before accepting them as official documents.
Automate PDF security during upload to streamline processing.

Conclusion

With TCPDF, FPDI, and FPDF, you can add digital signatures, watermarks, and security to PDFs in PHP.

Digitally sign PDFs to ensure authenticity.
Add text or image watermarks for branding.
Prevent editing and copying of sensitive PDFs.

By implementing these security measures, you can protect documents, prevent tampering, and automate PDF security in your PHP applications. 🚀

Leave a Reply