2012-10-03 42 views
2

我有一個Web應用程序,用戶可以在其中上傳PDF文檔。有沒有可用於密碼保護PDF文件的PHP庫?我需要庫來保存原始PDF的所有方面(即大小,字體,分辨率等)。如何以PHP密碼保護上傳的PDF

+6

你想密碼保護文件本身,或者只有當你嘗試從你的web應用程序打開它時? –

回答

4

Download the library used: Protect PDF in PHP

<?php 

function pdfEncrypt ($origFile, $password, $destFile){ 
//include the FPDI protection http://www.setasign.de/products/pdf-php-solutions/fpdi-protection-128/ 
require_once('fpdi/FPDI_Protection.php'); 

$pdf =& new FPDI_Protection(); 
// set the format of the destinaton file, in our case 6×9 inch 
$pdf->FPDF('P', 'in', array('6','9')); 

//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 
$password = '[email protected]'; 

//name of the original file (unprotected) 
$origFile = 'book.pdf'; 

//name of the destination file (password protected and printing rights removed) 
$destFile ='book_protected.pdf'; 

//encrypt the book and create the protected file 
pdfEncrypt($origFile, $password, $destFile); 
?> 

編輯 Original source of library used。請注意,我上面的答案沒有使用源代碼中的腳本進行測試。我從上面的第三方鏈接下載,我沒有檢查它們是否完全一樣。

+1

請指定您從何處下載庫的來源。該頁面將有更多的幫助,直接鏈接到圖書館。 – mccbala

+1

@mccbala看我的編輯。如果您發現此功能有用,請記住+1問題和答案。 –