Auto download mpdf generated pdf document

Auto download mpdf generated pdf document

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Auto Download MPDF Generated PDF Documents

The most common source of confusion when working with mpdf stems from a single method: $mpdf->Output(). In your current implementation, you are passing a filename string to this method. This tells the library to generate the PDF and then present it to the browser with a default action, which is almost always rendering the file in a new tab or prompting the user to save it manually.

There is no download() method on the MPDF class, nor should you expect one to exist in standard releases. The behavior you are seeing—opening the PDF in a tab—is the direct result of how the Output method handles its arguments. To force an automatic download, you must explicitly configure the HTTP headers that instruct the browser's rendering engine to treat the response as binary data requiring immediate storage.

The solution lies in manipulating the second parameter passed to $mpdf->Output(). This parameter is a flag that determines the output mode. By default, it is set to S (Save), which generates a file on the server but leaves the browser to decide how to display it based on the Content-Disposition header. To trigger an immediate download, you must pass D for Direct.

Here is how you modify your existing code:

include("../mpdf.php");
$html = "my HTML code here !";

$mpdf = new mPDF('c', 'A4', '', 32, 25, 27, 25, 16, 13); 
$mpdf->SetDisplayMode('fullpage');
$mpdf->list_indent_first_level = 0; 

$stylesheet = file_get_contents('mpdfstyletables.css');
$mpdf->WriteHTML($stylesheet, 1);
$mpdf->WriteHTML($html);

// The first parameter is the filename.
// The second parameter 'D' forces direct download.
$mpdf->Output('mpdf.pdf', 'D'); 

While passing D is often sufficient, relying solely on this can sometimes be unreliable across different browsers or server configurations because it depends on the Content-Disposition header being interpreted correctly. For a robust implementation that works consistently regardless of the browser's default settings, you should explicitly set the headers before generating the output. This approach gives you granular control over the download name and ensures the MIME type is strictly defined.

You can achieve this by accessing the underlying PHP stream or simply using the Output method with a specific filename while ensuring the headers are managed correctly. However, the most explicit way to guarantee a download behavior in older MPDF versions or specific server environments (like Apache) involves setting the Content-Disposition header directly in your PHP script before calling Output, though MPDF attempts to handle this internally when the mode is set to 'D'.

If you are working within a framework like Laravel, you might find yourself wrapping this logic inside a controller. In such cases, it is crucial to ensure that no other headers (such as those from view() rendering or middleware) have already been sent before attempting to output the binary PDF stream. If headers are already sent, PHP will throw a fatal error when trying to set additional headers for the download.

Another common pitfall involves file pathing and permissions. When using the 'D' mode with a relative filename like 'mpdf.pdf', MPDF writes the file to the current working directory of your web server (often the public or htdocs folder). If your application runs under a restricted user, this might fail if that folder lacks write permissions. A more secure pattern is to generate the PDF directly to php://output when using mode 'D' combined with explicit headers, bypassing the need to create an intermediate file on the disk entirely.

$mpdf->Output('downloaded_document.pdf', 'D'); 

By shifting from a passive file save to an active direct output mode, you align the server's response with the client's expectation of receiving a downloadable resource rather than viewing inline content. This distinction is fundamental to building robust PDF generation utilities that respect user intent without relying on browser heuristics.

Our network