Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can TCPDF / FPDI accept PDF as string?

Tags:

php

tcpdf

fpdi

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?

like image 677
jbrain Avatar asked Oct 29 '25 05:10

jbrain


2 Answers

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...

like image 98
Vincent Pazeller Avatar answered Oct 30 '25 23:10

Vincent Pazeller


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.

like image 43
NullColaShip Avatar answered Oct 30 '25 22:10

NullColaShip