Mpdf - 覆盖加密文件时出现问题
Mpdf - Problem overwriting encrypted files
我在用 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很棒,工具很多,但是生成文件需要很长时间。覆盖它们是唯一可以使最终用户在下载包含其个人数据的文件时获得良好体验的解决方案。如果有一个可以用加密覆盖的解决方案会更好,正如手册所说,这是可能的,但我自己无法解决...
好的,在考虑了评论并反复尝试了许多小时之后,我明白了。可以使用维护加密的 mpdf 生成 pdf 并用不同的 php 页面覆盖。当我们设置加密时,会生成变量 $uid、$oval、$encKey、$RC128、$uval 和 $pval。每当我们再次生成 pdf 时,这些变量都会发生变化,因此生成一次很重要。生成后,我们必须将这些变量的值发送到服务器。在用客户端数据覆盖 pdf 的页面上,保持加密,我们必须从服务器恢复这些变量,以便 mpdf 可以接受覆盖加密的 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' ) ;
?>
就是这样。我希望这段代码能像帮助我一样帮助其他人!
我在用 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很棒,工具很多,但是生成文件需要很长时间。覆盖它们是唯一可以使最终用户在下载包含其个人数据的文件时获得良好体验的解决方案。如果有一个可以用加密覆盖的解决方案会更好,正如手册所说,这是可能的,但我自己无法解决...
好的,在考虑了评论并反复尝试了许多小时之后,我明白了。可以使用维护加密的 mpdf 生成 pdf 并用不同的 php 页面覆盖。当我们设置加密时,会生成变量 $uid、$oval、$encKey、$RC128、$uval 和 $pval。每当我们再次生成 pdf 时,这些变量都会发生变化,因此生成一次很重要。生成后,我们必须将这些变量的值发送到服务器。在用客户端数据覆盖 pdf 的页面上,保持加密,我们必须从服务器恢复这些变量,以便 mpdf 可以接受覆盖加密的 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' ) ;
?>
就是这样。我希望这段代码能像帮助我一样帮助其他人!