Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Psr Http Message Shim Laravel Package

mpdf/psr-http-message-shim

PSR-7 HTTP message shim used by mPDF, providing lightweight interface-compatible request/response/message classes for projects that can’t rely on a full PSR-7 implementation. Helps maintain interoperability with minimal dependencies.

View on GitHub
Deep Wiki
Context7

Getting Started

This package provides a lightweight PSR-7/PSR-17 implementation shim for mpdf, bridging PSR HTTP messages and mPDF’s internal stream handling. Start by installing it via Composer:

composer require mpdf/psr-http-message-shim

The most common immediate use case is passing PSR-7 StreamInterface objects (e.g., from uploaded files or Symfony/Guzzle requests) directly to mPDF’s Output() method or constructors that accept streams—avoiding manual fopen()/fread() boilerplate.

Implementation Patterns

  • mPDF + PSR-7 Interoperability: When using mPDF in modern frameworks (Laravel, Symfony), inject a PSR-7-compliant StreamFactory (e.g., Nyholm/psr7 or GuzzleHttp\Psr7) and use the shim to convert streams for mPDF:
    $stream = $streamFactory->createStreamFromFile('/tmp/report.pdf');
    $mpdf = new \Mpdf\Mpdf();
    $mpdf->WriteHTML($html);
    $mpdf->Output($stream, \Mpdf\Output\Destination::STREAM); // Uses shim internally if available
    
  • Testable Output Handling: In unit tests, replace real file writes with in-memory PSR-7 streams to assert PDF content without filesystem side effects:
    $stream = (newStreamFactory())->createStream();
    $mpdf->Output($stream, Destination::STREAM);
    $pdfContent = $stream->getContents();
    $this->assertStringContainsString('%PDF-1.4', $pdfContent);
    
  • Framework Integration: In Laravel, bind a PSR-17 StreamFactory in AppServiceProvider and inject it where generating dynamic PDFs (e.g., from queued jobs), using the shim to unify stream sources.

Gotchas and Tips

  • No magic auto-detection: mPDF does not automatically use this shim unless explicitly passed a PSR-7 stream and the package is installed. Verify it’s in composer.json’s require (not just require-dev).
  • Output destinations matter: The shim only affects Destination::STREAM and Destination::DOWNLOAD. Use Destination::FILE or Destination::STRING for non-stream operations—this package won’t help there.
  • Version compatibility: Ensure mPDF ≥ v8.0.3 (shim support was added mid-v8). Older versions ignore it entirely.
  • Debugging tip: If PDF output seems empty or malformed, check if the stream is seekable—mPDF requires seekable streams. Use StreamWrapper::createStream() from GuzzleHttp\Psr7 if mocking with non-seekable sources.
  • Extension point: The package is tiny (~200 lines); review its MpdfPsrStreamTrait for low-level insight if subclassing mPDF or writing a custom output destination.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4