是否可以在codeigniter中创建密码保护的PDF文件?我需要使用密码来保护我通过电子邮件发送的PDF文件。
任何人都知道这样做吗?
答案 0 :(得分:1)
在mpdf库中按照https://mpdf.github.io/reference/mpdf-functions/setprotection.html#examples
使用它 <?php
$mpdf = new mPDF();
// Encrypt the file and grant no permissions to the user to copy, print etc.
// The user will be able to open the file as no password is specified
// Owner cannot access full rights because no owner_password was set
$mpdf->SetProtection(array(), 'UserPassword', 'MyPassword');
$mpdf->WriteHTML('
Hallo World
');
$mpdf->Output('filename.pdf');
?>
邮件使用此示例。
Example - Sending file as e-mail (and also to browser)
<?php
$mpdf = new mPDF();
$mpdf->WriteHTML($html);
$mpdf->SetProtection(array(), 'UserPassword', 'MyPassword'); //set password
$content = $mpdf->Output('', 'S');
$content = chunk_split(base64_encode($content));
$mailto = 'recipient@domain.com';
$from_name = 'Your name';
$from_mail = 'sender@domain.com';
$replyto = 'sender@domain.com';
$uid = md5(uniqid(time()));
$subject = 'Your e-mail subject here';
$message = 'Your e-mail message here';
$filename = 'filename.pdf';
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/pdf; name=\"".$filename."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
$is_sent = @mail($mailto, $subject, "", $header);
$mpdf->Output();