Search code examples
phpmpdfphp-7.2

mPDF SetSourceFile - import PDF from temporary storage


I'm trying import PDF file with mPDF. PDF file is sent from other service, and my initial idea was to use wrappers. (preferably php://memory).

What errors I get:

Warning: filesize(): stat failed for php://memory in /var/www/scrm/modules/AOS_PDF_Templates/PDF_Lib/mpdfi/pdf_parser.php on line 181

mPDF error: Cannot open php://memory ! (this one is printed to browser BTW)

I have PDF checked:

  • displayed in browser
  • saved to file, and imported from it

Both work, so PDF is not an issue.

I have checked contents of php://memory, they are valid as well. Here is example code:

//part of the code, $content is defined earlier (.pdf file content)
$memoryFile = 'php://memory';
$handle = fopen($memoryFile,'r+');
fwrite($handle,$content);
rewind($handle);

/* dump it to string, just to be sure, part of how i was checking that pdf got saved to $memoryFile
   even with this part removed still doesn't work, so no file issue here
$str=  fread($handle,strlen($content)); //str gets filled with expected data
rewind($handle); //rewind for use by mPDF
*/

$pdf = new mPDF;
$pdf->SetImportUse();
$pagecount = $pdf->SetSourceFile($memoryFile);

Solutions I have in mind:

  • save to regular temp file (I don't want to have to do this)
  • is there a way for mPDF to import file from string? (Google yielded no results for me)

OS: Ubuntu 18.04 WSL, PHP version 7.2.31, mPDF version 5.7.1


Solution

  • Upgrade your mPDF version at least to 8.0.0.

    Since version 8, mPDF uses updated underlying library FPDI 2 that supports importing files from php://memory or any stream, generally.