0

I am having some trouble using the setasign/fpdf and setasign/fpdi with fpdf/fpdf to password protect an uploaded pdf file.

So far I have this in my composer.json

{
    "require": {
        "phpmailer/phpmailer": "^6.8",
        "fpdf/fpdf": "^1.85",
        "setasign/fpdf": "1.8.*",
        "setasign/fpdi": "^2.0",
        "setasign/fpdi-protection": "^2.0"
    }
}

and this on the page that handles the php.

<?php
require_once('vendor/autoload.php');
use setasign\Fpdi\Fpdi;
$pdf = new Fpdi();
$pageCount = $pdf->setSourceFile('book.pdf');
$tplIdx = $pdf->importPage(1);
$pdf->AddPage();
$pdf->useTemplate($tplIdx);
$permissions = array(
    'print' => false,
    'copy' => false,
    'modify' => false,
    'annot-forms' => false
);
$pdf->SetProtection($permissions);
$pdf->Output('protected_pdf.pdf', 'D');

but then I get the following error: Fatal error: Uncaught Error: Class 'Fpdi' not found in /home/

If i omit $pdf->SetProtection($permissions); then the script runs normally and the pdf is outputted.

require_once('vendor/autoload.php');

use setasign\Fpdi\Fpdi;

require_once('vendor/setasign/fpdf/fpdf.php');
require_once('vendor/setasign/fpdi/src/autoload.php');
require_once('vendor/setasign/fpdi-protection/src/autoload.php');

$pdf = new Fpdi();
$pageCount = $pdf->setSourceFile('book.pdf');
$tplIdx = $pdf->importPage(1);
$pdf->AddPage();
$pdf->useTemplate($tplIdx);
$permissions = array(
    'print' => false,
    'copy' => false,
    'modify' => false,
    'annot-forms' => false
);
$pdf->SetProtection($permissions);
$pdf->Output('protected_pdf.pdf', 'D');

also gives the same error.

<?php

require_once('vendor/autoload.php');
require_once('vendor/fpdf/fpdf/src/Fpdf/Fpdf.php');
require_once('vendor/setasign/fpdf/fpdf.php');
require_once('vendor/setasign/fpdi/src/autoload.php');
require_once('vendor/setasign/fpdi-protection/src/FpdiProtection.php');
require_once('vendor/tecnickcom/tcpdf/tcpdf.php');

use setasign\Fpdi\Fpdi;

$sourceFile = 'book.pdf';
$outputFile = 'book2.pdf';

$password = 'your_password';

$pdf = new FPDI();

$pageCount = $pdf->setSourceFile($sourceFile);

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

$pdf->SetProtection(array(), $password);

$pdf->Output($outputFile, 'F');

also tried.

1 Answers1

1

I don't think that you get the same error message in all examples but they are different. The method setProtection() is not available in the default FPDI class. Stay at the composer installation and use the class of FpdiProtection:

<?php
use setasign\FpdiProtection\FpdiProtection;

// setup the autoload function
require_once('vendor/autoload.php');

$pdf = new FpdiProtection();
$ownerPassword = $pdf->setProtection(
    FpdiProtection::PERM_PRINT | FpdiProtection::PERM_COPY,
    'the user password',
    'the owner password'
);
Jan Slabon
  • 4,736
  • 2
  • 14
  • 29