Can TCPDF / FPDI accept PDF as string?

半世苍凉 提交于 2019-12-12 14:43:14

问题


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?


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




回答2:


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




回答3:


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');


来源:https://stackoverflow.com/questions/12497408/can-tcpdf-fpdi-accept-pdf-as-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!