I am building a simple web service that needs to join two PDFs into one PDF file. I can pass both PDFs as text(base encode 64) with a POST to the service and the service needs to spit out the combined PDF as encoded text, The client will then re-encode and make the PDF.
I found this question here which got me started: Can TCPDF / FPDI accept PDF as string?
I found some sample code and the multiple libraries:
- FPDI Example
- TCPDI<-setSourceData(string)
- TCPDF
Here is an encoded PDF single page simple.(Post data) http://pastebin.com/zLXmCNJt
Here's my code:
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/FPDF/tcpdf/tcpdf.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/FPDF/tcpdf/tcpdi.php');
class ConcatPdf extends TCPDI
{
public $files = array();
public function setFiles($files)
{
$this->files = $files;
}
public function concat()
{
foreach($this->files AS $file) {
//$pageCount = $this->setSourceFile($file);
$pageCount = $this->setSourceData($file);//i modified this
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); //error here
}
}
}
}
$pdf641 = $_POST['pdf1'];
$pdf642 = $_POST['pdf2'];
$pdf = new ConcatPdf();
$pdf->setFiles(array(base64_decode($pdf641), base64_decode($pdf642) ));
$pdf->concat(); //!!error here!!
echo base64_encode($pdf->Output('concat.pdf', 'S'));
?>
Here is the error log from the server: http://pastebin.com/q2vzZfft
What's going wrong here? How do I fix this?