dongtuojuan8998 2017-05-08 07:13
浏览 74

密码保护对于PHP中的pdf

Developing an Admin page, in which admins uploads PDF and user downloads that PDF. After user downloads PDF, the PDF has to ask password, how to set password for that PDF automatically in php and that PDF should not set by Admin

This is my Admin Page Code

  move_uploaded_file($tmp_name, $location);
if (copy($location, $location1)) {

    $query = mysqli_query($conn, "SELECT * FROM `at_uploads` WHERE  name='$name1'");
    if (mysqli_num_rows($query) > 0) {

        echo("<SCRIPT LANGUAGE='JavaScript'>
                     window.alert('File already exists with that name')
                     window.location.href='upload';
                      </SCRIPT>");
    }
}

This is my user download page

if(isset($_GET['dow'])) {

$filepath1 = $_GET['dow'];

$query = mysqli_query($conn, "SELECT * FROM `at_uploads` WHERE filepath1 ='$filepath1'");
while (($row = mysqli_fetch_array($query))) {

    $name=$row['name'];
    $location = "C:/wamp/www/school/admin/files/" . $name;
    header('Content-Type: application/pdf');

    header('Content-Disposition: attachment; filename="' . basename($location) . '"');
    header('Content-Length: ' . filesize($location));
    readfile($location);
      }
  }

I don't know where to set password for pdf

  • 写回答

1条回答 默认 最新

  • dongtong5242 2017-05-08 07:19
    关注

    Which library are you using? If you are using FPdi, please use the function below.

    <?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 = 'info@yourdomain.com';
    
    //name of the original file (unprotected)
    $origFile = 'yourfilename.pdf';
    
    //name of the destination file (password protected and printing rights removed)
    $destFile ='yourfilename.pdf';
    
    //encrypt the book and create the protected file
    pdfEncrypt($origFile, $password, $destFile );
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题