3

Is it possible to feed TCPDF or FPDI PDFs as a string? I have an incoming array of PDFs as strings, and I can't write to disk. I wasn't able to find anything in the documentation about this.

If not, is there an efficient way to store/read these PDFs from memory or as objects? as to feed them to FPDI?

jbrain
  • 561
  • 3
  • 7
  • 18
  • Do you mean PDF file names as strings in an array? Or some sort of stream of raw PDF data encapsulated in an array? – batfastad Sep 19 '12 at 15:33
  • You could write it to a `php://temp` stream, and then read that into TCPDF -- see http://php.net/manual/en/wrappers.php.php – SDC Sep 19 '12 at 15:54
  • SDC: I went with a similar approach, I compiled and installed memcache and am storing the files there temporarily, but I can't get fpdi/tcpdf to read from memcache, it's determined to only read from files. Is there a way to get memcache keys to behave as file handles? – jbrain Sep 20 '12 at 13:29

3 Answers3

7

If you look at the setSourceFile method documentation, you will see that you can also pass a resource or a StreamReader. What is very interesting with the StreamReader is that it also shares a createByString method. So you can use it like this:

use setasign\Fpdi\PdfParser\StreamReader;
//...
$myData = ... ;
$stream = StreamReader::createByString($myData);
$pdf->setSourceFile($stream);
//...

This will avoid any code duplication... hope this helps someone in the future...

Vincent Pazeller
  • 1,448
  • 18
  • 28
1

FPDI does not accept strings, but TCPDI, which I just released, has a setSourceData() method in addition to FDPI's setSourceFile(), as I happened to have the exact same requirement. TCPDI has its own parser (tcpdi_parser, based on TCPDF's parser) which supports PDFs above 1.4 without requiring the commercial addon for FPDI - which may also be of benefit when working with existing PDFs.

NullColaShip
  • 368
  • 3
  • 12
  • I've since used this (TCPDI) in another project. Works quite well! – jbrain Feb 12 '14 at 20:42
  • @nullColaShip I'm using setSourceData() and getting the error: `TCPDI_PARSER ERROR [tcpdi-56a25ce24b2d6]: Unable to find startxref` – GisMofx Jan 22 '16 at 16:50
1

you can used stream wraper ,..

you can write wraper.php from class link above

header('Content-Type: text/html; charset=utf-8');

require_once('tcpdf/tcpdf.php');
require_once('fpdi/fpdi.php');
require_once('wraper.php');

// Creating new page with PDF as a background
$pdf = new FPDI();
$varInAnyScope = file_get_contents('proposal0.pdf');
$pdf->setSourceFile(VarStream::createReference($varInAnyScope));
$tplIdx = $pdf->importPage(1);
$pdf->AddPage();
$pdf->useTemplate($tplIdx, 0, 0, 0, 0, true);

$pdf->Write(0, "Stack overflow");

ob_clean();
$pdf->Output('WorksheetTest.pdf', 'I');
Triyan
  • 21
  • 2