我有一个PHP脚本合并两个PDF文件。当我直接从服务器提取这两个文件时,该脚本工作正常。但重点是发布两者将其视为服务,并将两个文件作为基本64位编码文本发布,然后合并文件,让接收端下载合并的PDF。
使用post变量时,我得到了混合结果。我认为某些东西卡在缓存或会话中,但我不太熟悉如何正确地实现这一点。
当结果不正确时,首先将PDF组合两次。我开始检查PDF1 = PDF2是否会抛出错误,但是在测试时它永远不会发生,除非我实际发布相同的PDF。我已经在脚本中尝试了进一步的测试,但无法确定错误点。
我还可以调试这个吗?我还能尝试什么?
这是当前的脚本:
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/PDFMerge/tcpdf/tcpdf.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/PDFMerge/tcpdf/tcpdi.php');
class MergePDF extends TCPDI
{
public $files = array();
public function setFiles($files)
{
$this->files = $files;
}
public function concat()//from set assign example
{
foreach($this->files AS $key=> $file) {
$pageCount = $this->setSourceData($file);//setSourceData used for raw data
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
$tplIdx = $this->ImportPage($pageNo);
$s = $this->getTemplatesize($tplIdx);
$this->AddPage($s['w'] > $s['h'] ? 'L' : 'P', array($s['w'], $s['h']));
$this->useTemplate($tplIdx);
}
if($key>0){
if ($this->files[0] == $this->files[1]){
echo "Duplicate file error"; //I cannot get this to occur
die();
}
}
unset($file);
unset($pageCount);
}
}
}
//error_log("MergePDF Called");
$pdf641 = $_POST['pdf1'];
$pdf642 = $_POST['pdf2'];
$outputType = $_POST['output'];
if ($pdf641 == $pdf642)
{
echo "error: both files are the same";
error_log("MergePDF error: Both Files are The Same!");
session_unset();
die();
}
/*
if (hash("sha256",$pdf641,false) == hash("sha256", $pdf642,false))
{
echo "error: both files are the same";
error_log("MergePDF error: Both SHA256 Files are The Same!");
die();
}
*/
$p1d = base64_decode($pdf641);
$p2d = base64_decode($pdf642);
$pdf = new MergePDF();
$pdf->setFiles(array( $p1d, $p2d));
$pdf->concat();
$uuid = uniqid("pdf_",false);
if ($outputType == "download")
{
$pdf->Output($uuid . '.pdf', 'I'); /*//D I*/
}
else
{
$out = base64_encode($pdf->Output($uuid . '.pdf', 'S')); //output is 'S' for string
echo $out;
}
?>