Mpdf 'D' failing

I am trying to use the mpdf library to generate a pdf file and then send it directly to the client as a download, i.e. use the ‘D’ option in mpdf->Output - this gives an error message - see below. ( Alternatively i’d be happier using the ‘I’ option and send in line to the browser - but nothing appears to happen when i try this). using the ‘f’ option to generate a file works well.

I get a fatal error 'Data has already been sent to output, unable to output PDF file ’ . This problem is reported on the mpdf web site - i have followed the advice there but it hasn’t solved the problem.

with the following stack trace;
Fatal error : Uncaught Mpdf\MpdfException: Data has already been sent to output, unable to output PDF file in /var/www/ Stack trace: #0

/var/www/ Mpdf\Mpdf->Output(‘gad796pdf’, ‘D’) #1

/var/www/ GeneratePDF(28, ‘96’) #2

/var/www/ gad7_report(28, 130, 2, ‘96’) #3

{main} thrown in /var/www/ on line 9543

when i call ob_get_contents() it returns a string which is correctly displayed, calling ob_end_clean() does not help with the problem.

any help would be much appreciated, thanks ruth

I’ve managed to get mpdf-Output(filename,D) to work, i.e. there is no problem with the mPDF library called within OpenEMR.

I’m not entirely sure why, or rather what the problem was. I’ve re-arranged the order in which I call the function to generate the pdf and at the moment only have a hazy understanding of the difference this has made !!

1 Like

Hi Ruth,

All you’re doing here is grab your buffer which hasn’t been outputted to browser yet. What you experienced is somehow a write leaked(by this I mean not meaning to) output to network/browser.
Perhaps moving call to mPDF generation was after this write, maybe a notification to user or such…

Thanks Jerry - yes this makes sense (i think!) - anyway i am now generating pdf’s with ‘D’ (i.e. download) or ‘I’ (display in a new window) when the user requests it so am pleased!

lol, Most likely a mistake you won’t repeat! After several years working in PHP, I still make this mistake if it makes you feel any better.

Always fun when stuff we create works.:slight_smile: Do More…

haha, thanks - it does make me feel better!! still getting my head
round server/client separation, what gets executed where and when,
how to go back and forth between them - but interesting! and
enjoyable (especially when stuff works - as you say!)

Try to remember that PHP/server formats/builds/populates content that the browser parses and displays. So think of it as the server tells browser what to do more or less.

thanks jerry

i don't know if you can help with my next problem: i was happily

generating pdfs either for download or display in a browser window
(‘d’ or ‘i’) when i started to get the following error, but not on
every occasion. This seems ‘intermittent’ in that i cannot cause it
nor prevent it from happening. I do not understand it so am
struggling to find the cause. ‘report-as-pdf.php’ is my code to
generate the pdf, the error occurs for ‘d’ and ‘i’. What is
‘symfony’ and it’s role in the process, i guess this is to do with
listening for events? I don’t really know how mpdf works, is this
being caused in my code or mpdf’s etc…(looking at dispatcher code
isn’t helping me (yet?)) :

Fatal error : Uncaught Mpdf\MpdfException: Error detected. PDF
file generation aborted: Calling the
method with the event name as the first argument is deprecated since
Symfony 4.3, pass it as the second argument and provide the event
object as the first argument instead. in
Mpdf\Mpdf->Output(‘Shazia_Brown_ga…’, ‘I’)
GeneratePDF(28, ‘96’)
#2 {main} thrown in /var/www/
on line **58

** many thanks in anticipation

so, as an example, the code is working currently, but if i delete a
statement, which is a dummy statement assigning to a string, i
generate the error. i can make no sense of this.
reminds me of the good old days when the position of code in memory
could have an effect !!!


Hi @ruth, feel free to upload your report-as-pdf.php script.

thanks, stephen, here it is,

on our system if one comments out line 137 the error is generated,

as it is a pdf is generated,


report-as-pdf.php (6.43 KB)

thanks, there’s a funny </he> tag on line 111, prob should be </h2> and also line 114

here’s a psr12’ed version with the h2 fixes
report-as-pdf.php (6.7 KB)

thanks stephen,

ah - i had spotted the one on line 114 but not this one.  I've

changed the ‘he’ into the ‘h2’ it should have been (a typo!) - but
it makes no difference to the appearance of the error message


and another clue: If i move the dummy line 137 until after the call
to the function GeneratePDF the code crashes - (so something about
space for storing variables maybe but i don’t know enough about how
php is interpreted…)

will keep thinking... it's quite intriguing!


hi @ruth, do you have an example of where you call this function? Maybe you’d like to create another PR and add this to the gad7 form?

hi stephen, thanks - a good idea - i’ve been wondering if the
problem is sytem dependant too - and this reminded me i can try it
out with docker locally as well.
will do soon. ruth

Symfony is not a dependency of mPDF that i’m aware. So, the error is most likely bogus!
First L-137 will error simply because $client_name_fn is defined in a function and doesn’t have scope where used. Remove if not used or wanted otherwise get variables in order. Possible stack corruption.

Also, L-140 is outputting to buffer before calling mPDF gen function and I don’t see a reason for it and if wanted in document, then include in gen function. I don’t see an output buffer set up and besides with the allow_output_buffering set to true then mPDF will ignore errors from outputting to object buffer anyway. Turn off may give you a better error if buffer issues.

Another possible problem is the L-130 MpdfException is an undefined class. Either import the class or prefix the class with \. i.e. catch (\MpdfException $exception).

W/o full context of how this file is included or used in project, difficult to tell much.

hi stephen,
thanks. I’ve just created another PR. (hope i did it in correct

If you  looks at the report of a GAD7 form in an encounter you will

see a blue link below the data which says ‘Generate PDF’ - clicking
on this executes the file report-as-pdf.php on the server.

as i've uploaded it this should crash as i described earlier, with

the message referring to symfony components.
If you uncomment line 138 then it should work and generate the pdf
for download or inline display in a browser window (depending on
global value for output type in pdf parameters)… there is a second
line at 140 which has no effect - just me trying to nudge things.

Jerry also had a look at the code, I went through his suggestions

with no greater success. As he says it looks like a stack
corruption. Just to note that line 138 is not a necessary piece of
code, it is just there to prevent the module from crashing. I think
you will have seen his message?

many thanks for your help


p.s. it's not necessarily the final version...