我可以使用 mpdf 或任何其他 php pdf 库为已生成的 pdf 文件设置密码吗

Can i set password for already generated pdf file using mpdf or any other php pdf libraries

我需要在 php 中使用 mpdf 为我已经生成的 pdf 文件设置密码。

  1. 我在 php.
  2. 中使用 imagick() 扩展将我已经生成的 pdf 转换为 jpeg
  3. 我再次使用 TCPDF 将 jpeg 转换为 pdf。
  4. 我在使用 TCPDF 将 jpeg 转换为 pdf 时设置了密码。

对不起最差的英语。

下面是我的代码:

//将pdf转为jpg $imagick = new imagick();

            $imagick->readImage(base_url().'pdffiles/'.$faxid.'.pdf');

            foreach($imagick as $i => $imagick) {

                $imagick->setImageFormat('jpeg');

                $imagick->setResolution(300,300);

                header('Content-Type: image/jpeg');

                $path1 = './pdffiles/'.$faxid.'_'.$i.'.jpg';

                file_put_contents($path1, $imagick ); 
            }

            // Convert jpg to pdf with password protection
            $this->load->library('Tc_pdf');
            $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT,  true, 'UTF-8', false);

            $pdf->SetProtection(array('print', 'copy','modify'), $loggedInUser, "ourcodeworld-master", 0, null);

            $pdf->SetCreator(PDF_CREATOR);

            $pdf->SetFont('courier', '', 20);
            $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

            //$dir = base_url()."/pdffiles/";
            $map = directory_map('./pdffiles/');
            foreach ($map as $key => $value) {
                $extention = explode('.', $value);
                if($extention[1] == 'jpg') {
                  $pdf->AddPage();
                  $img = file_get_contents('./pdffiles/'.$value);
                  $pdf->Image('@' . $img, 20, 20, '', '', 'JPG', '', 'T', false, 100, '', false, false, 0, false, false, false);

                }

            }

            foreach ($map as $key => $value) {
                @unlink('./pdffiles/'.$value);
            }   

            $pdf->Output('example.pdf', 'I');