J'utilise html2fpdf pour créer des documents PDF. Maintenant, une fois que j'ai créé cela, je veux m'assurer que le fichier PDF est protégé par mot de passe. Comment cela peut-il être fait en PHP?Comment créer un fichier pdf protégé par mot de passe
3
A
Répondre
5
Téléchargez la bibliothèque, je me sers de a blog post on the ID Security Suite site:
<?php
function pdfEncrypt ($origFile, $password, $destFile){
require_once('FPDI_Protection.php');
$pdf =& new FPDI_Protection();
$pdf->FPDF('P', 'in');
//Calculate the number of pages from the original document.
$pagecount = $pdf->setSourceFile($origFile);
//Copy all pages from the old unprotected pdf in the new one.
for ($loop = 1; $loop <= $pagecount; $loop++) {
$tplidx = $pdf->importPage($loop);
$pdf->addPage();
$pdf->useTemplate($tplidx);
}
//Protect the new pdf file, and allow no printing, copy, etc. and
//leave only reading allowed.
$pdf->SetProtection(array(), $password);
$pdf->Output($destFile, 'F');
return $destFile;
}
//Password for the PDF file (I suggest using the email adress of the purchaser).
$password = "testpassword";
//Name of the original file (unprotected).
$origFile = "sample.pdf";
//Name of the destination file (password protected and printing rights removed).
$destFile ="sample_protected.pdf";
//Encrypt the book and create the protected file.
pdfEncrypt($origFile, $password, $destFile);
?>
0
Je n'ai jamais été capable de trouver une solution php directe à ce problème. J'ai fini par utiliser pdftk et en utilisant shell_exec()
pour appeler le fichier binaire une fois que le fichier pdf a été généré/téléchargé.
Il accepte une syntaxe comme ceci:
pdftk 'inputfile.pdf' output 'outputfile.pdf' user_pw pass1234 owner_pw pass4321
+0
i it..Try obtenu @ Mark ce que je suis annonce ici réponse. –
+0
bleee, logiciel Windows –
nous pouvons voir ce fichier crypté dans Windows 8 sans mot de passe. Ceci est un échec dans Windows 8. Y at-il une solution pour cela. Merci – user2092317