我在用mpdf覆盖加密文件时遇到了很大的问题。而且我需要覆盖它们而不是每次都生成它们,因为生成文件需要花费很长时间,而覆盖它们是一种强大的策略,可能需要几秒钟而不是十分钟。下面显示的示例基于此hyperlink的示例2的解决方案。实际上,当生成它们而没有加密时,我可以覆盖它们。但是,每当我尝试加密时,我都会得到一个空白页面,看起来似乎没有用。而且加密对于避免盗版至关重要。
用于生成pdf的代码如下:
<?php
require_once '..\www\vendor\autoload.php';
$mpdf = new mPDF();
$mpdf->percentSubset = 0;
$mpdf->SetProtection(array(), '', 'bread'); // Need to specify a password
$mpdf->WriteHTML('This copy is XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
$mpdf->Output('test.pdf','F');
// Have to save various encryption keys, which are uniquely generated each document
$uid = $mpdf->uniqid;
$oval = $mpdf->Ovalue;
$encKey = $mpdf->encryption_key;
$uval = $mpdf->Uvalue;
$pval = $mpdf->Pvalue;
$RC128 = $mpdf->useRC128encryption;
unset($mpdf);
?>
用于覆盖原始文件的代码如下:
<?php
error_reporting(0); // had to include this to open with google chrome
ini_set('display_errors', 0); // had to include this to open with google chrome
ob_clean(); // had to include this to open with google chrome
require_once '..\www\vendor\autoload.php';
$mpdf = new mPDF();
$mpdf->SetImportUse();
// Re-instate saved encryption keys from original document
$mpdf->encrypted = true;
$mpdf->useRC128encryption = $RC128;
$mpdf->uniqid = $uid ;
$mpdf->Ovalue = $oval ;
$mpdf->encryption_key = $encKey ;
$mpdf->Uvalue = $uval ;
$mpdf->Pvalue = $pval ;
$search = array(
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
);
$replacement = array(
"personalised for Jos\xc3\xa9 Bloggs"
);
$mpdf->OverWrite('test.pdf', $search, $replacement, 'I', 'mpdf.pdf' ) ;
?>
我非常感谢并感谢您解决此问题的方法! Mpdf非常棒,具有许多工具,但是生成文件花费的时间很长。覆盖它们是使最终用户拥有下载带有其个人数据的文件的良好体验的唯一解决方案。最好有一个可以用加密覆盖的解决方案,因为该手册说这是可能的,但是我无法解决自己的问题...
答案 0 :(得分:0)
好吧,我是在考虑了评论以及经过数小时的尝试和重试之后才明白的。可以使用维护安全性的mpdf生成pdf并用不同的php页面覆盖。设置加密时,将生成变量$ uid,$ oval,$ encKey,$ RC128,$ uval和$ pval。每当我们再次生成pdf时,这些变量都会更改,因此生成一次很重要。生成后,我们必须将这些变量的值发送到服务器。在使用客户端数据覆盖pdf进行维护加密的页面上,我们必须从服务器恢复这些变量,以便mpdf可以接受覆盖已加密的pdf来覆盖已加密的pdf。
下面是生成加密的pdf并将变量发送到服务器的代码:
<?php
define('MPDF_PATH','class/mpdf/');
include("../mpdf60/mpdf.php");
$mpdf = new mPDF();
error_reporting(0);
ini_set('display_errors', 0);
ob_clean();
$mpdf->SetImportUse();
$pagecount = $mpdf->SetSourceFile("testb.pdf");
$mpdf->SetDocTemplate('testb.pdf',true);
for ($i=1; $i<=$pagecount; $i++) {
$import_page = $mpdf->ImportPage($i);
$mpdf->UseTemplate($import_page);
if ($i < $pagecount)
{$mpdf->AddPage();
}}
$mpdf->percentSubset = 0;
$mpdf->SetProtection(array(), '', 'yourpassword'); // Need to specify a password
$mpdf->WriteHTML('This copy is XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
$mpdf->Output('test.pdf','F');
// Have to save various encryption keys, which are uniquely generated each document
$uid = $mpdf->uniqid;
$oval = $mpdf->Ovalue;
$encKey = $mpdf->encryption_key;
$uval = $mpdf->Uvalue;
$pval = $mpdf->Pvalue;
$RC128 = $mpdf->useRC128encryption;
unset($mpdf);
//==============================================================
$mpdf = new mPDF();
$mpdf->SetImportUse();
// Re-instate saved encryption keys from original document
$mpdf->encrypted = true;
$mpdf->useRC128encryption = $RC128;
$mpdf->uniqid = $uid ;
$mpdf->Ovalue = $oval ;
$mpdf->encryption_key = $encKey ;
$mpdf->Uvalue = $uval ;
$mpdf->Pvalue = $pval ;
//Here is the code to update the variables on your server. It is important to already have created a table and have filled the first line with some text in order to update properly
$sql2= "UPDATE `yourtable` SET `uid`='$uid', `oval`='$oval', `encKey`='$encKey', `uval`='$uval', `pval`='$pval', `RC128`='$RC128' WHERE `pdfline`='firstpdf'";
$result2 = mysqli_query($users, $sql2);
?>
然后,覆盖加密文件的代码如下:
<?php
define('MPDF_PATH','class/mpdf/');
include("../mpdf60/mpdf.php");
$nomestep1 = mysqli_query ($users, "SELECT `uid` FROM yourtable WHERE `pdfline`='firstpdf'");
$nomestep2 = mysqli_num_rows ($nomestep1);
for ($i=0 ; $i<$nomestep2 ; $i++)
{
$nomestep3 = mysqli_fetch_row($nomestep1);
echo $nomestep3[0];
}
$uid = $nomestep3[0];
// In order to illustrate, I showed the recovery from the server of just one variable. The others can be obtained using the same process
$mpdf = new mPDF();
$mpdf->SetImportUse();
// Re-instate saved encryption keys from original document
$mpdf->encrypted = true;
$mpdf->useRC128encryption = $RC128;
$mpdf->uniqid = $uid ;
$mpdf->Ovalue = $oval ;
$mpdf->encryption_key = $encKey ;
$mpdf->Uvalue = $uval ;
$mpdf->Pvalue = $pval ;
$search = array(
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
);
$replacement = array(
"personalised for Jos\xc3\xa9 Bloggs"
);
error_reporting(0);
ini_set('display_errors', 0);
ob_clean();
$mpdf->OverWrite('test.pdf', $search, $replacement, 'I', 'mpdf.pdf' ) ;
?>
就这样。我希望这段代码可以像帮助我一样帮助其他人!