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

问题描述 投票:0回答:1

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

php mpdf
1个回答
0
投票
  1. 我使用php中的imagick()扩展将我已生成的pdf转换为jpg。
  2. 我再次使用TCPDF将jpeg转换为pdf。
  3. 我在使用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');
© www.soinside.com 2019 - 2024. All rights reserved.