dompdf icon indicating copy to clipboard operation
dompdf copied to clipboard

Call to a member function get_content_box() on null

Open gabrielrbarbosa opened this issue 1 year ago • 18 comments

This issue is not happening in 3.1.0 only in latests updates. I get this error message when using any img in PDF, so it it is probably related to this changes here: https://github.com/dompdf/dompdf/pull/3500

gabrielrbarbosa avatar Sep 23 '25 14:09 gabrielrbarbosa

I'll take a look and see if I can track down the issue.

bsweeney avatar Sep 23 '25 15:09 bsweeney

Do you have the full stack trace and/or other log messaging? If not, you might be able to get some by using the troubleshooting configuration describe in the wiki.

Also, can you share sample HTML + CSS to reproduce the issue?

bsweeney avatar Sep 23 '25 15:09 bsweeney

dd($e->getTraceAsString());

#0 /var/www/httpdocs/v3/vendor/dompdf/dompdf/src/FrameReflower/Block.php(236): Dompdf\FrameReflower\Block->_calculate_width(227.2)
#1 /var/www/httpdocs/v3/vendor/dompdf/dompdf/src/FrameReflower/Block.php(811): Dompdf\FrameReflower\Block->_calculate_restricted_width()
#2 /var/www/httpdocs/v3/vendor/dompdf/dompdf/src/FrameDecorator/AbstractFrameDecorator.php(915): Dompdf\FrameReflower\Block->reflow(NULL)
#3 /var/www/httpdocs/v3/vendor/dompdf/dompdf/src/FrameReflower/Inline.php(161): Dompdf\FrameDecorator\AbstractFrameDecorator->reflow(NULL)
#4 /var/www/httpdocs/v3/vendor/dompdf/dompdf/src/FrameDecorator/AbstractFrameDecorator.php(915): Dompdf\FrameReflower\Inline->reflow(NULL)
#5 /var/www/httpdocs/v3/vendor/dompdf/dompdf/src/FrameReflower/Page.php(141): Dompdf\FrameDecorator\AbstractFrameDecorator->reflow()
#6 /var/www/httpdocs/v3/vendor/dompdf/dompdf/src/FrameDecorator/AbstractFrameDecorator.php(915): Dompdf\FrameReflower\Page->reflow(NULL)
#7 /var/www/httpdocs/v3/vendor/dompdf/dompdf/src/Dompdf.php(815): Dompdf\FrameDecorator\AbstractFrameDecorator->reflow()
#8 /var/www/httpdocs/v3/vendor/barryvdh/laravel-dompdf/src/PDF.php(244): Dompdf\Dompdf->render()
#9 /var/www/httpdocs/v3/vendor/barryvdh/laravel-dompdf/src/PDF.php(188): Barryvdh\DomPDF\PDF->render()
#10 /var/www/httpdocs/v3/vendor/barryvdh/laravel-dompdf/src/PDF.php(229): Barryvdh\DomPDF\PDF->output()

gabrielrbarbosa avatar Sep 23 '25 17:09 gabrielrbarbosa

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>{{ $title }} </title>
    @php use Illuminate\Support\Number; @endphp
</head>
<body>
<table class="header">
        <thead>
            <tr>
                <th rowspan="3" class="text-center" style="width: 8%;padding: 0; margin: 0;">
                    <img src="data:image/jpeg;base64,{{ $logo }}" alt="Logo" style="width: 38px;height: 37px;">
                </th>
                <th colspan="2"/>
            </tr>
        </thead>
    </table>
    <div style="position: absolute; bottom: 0; left: 0; width: 100%;">
        <table class="footer">
            <tr>
                    <img src="data:image/jpeg;base64,{!! base64_encode(file_get_contents('https://.../img/logo.jpg')) !!}" alt="Logo" style="width: 35px;height: 15px;">
                </th>
            </tr>
        </table>
    </div>
</body>
</html>

gabrielrbarbosa avatar Sep 23 '25 17:09 gabrielrbarbosa

Tracing the error this looks like an issue with positioned content not with file retrieval, so I don't think the curl change is related. However, I can't be sure because I was unable to reproduce with the sample you provided.

I can trigger the same error by forcing the body element into a non-block element (e.g., body { display: inline !important; }), which also triggers the error in 3.1.0. Does the sample you provided trigger the error for you? If not, is there additional styling that may be implicated?

bsweeney avatar Sep 24 '25 14:09 bsweeney

Following up to see if anyone has additional information to help identify the cause.

bsweeney avatar Oct 04 '25 15:10 bsweeney

Same for me, only occurred once, so it's weirdly intermittent.

Message: Call to a member function get_content_box() on null File: /var/task/vendor/dompdf/dompdf/src/FrameReflower/Block.php Line: 96

d-shannon avatar Oct 08 '25 19:10 d-shannon

Is there any movement on this or atleast an idea of what is causing it? I've done a fair amount of testing and I've not been able to replicate a scenario. Yet it is happening as we are seeing logs for it.

msumdev avatar Oct 16 '25 10:10 msumdev

Hi, to give more info, here is a stack trace from Symfony from our pipeline.

Error: Call to a member function get_content_box() on null
/builds/alpinresorts/dev/projects/odin/vendor/dompdf/dompdf/src/FrameReflower/Block.php:96
/builds/alpinresorts/dev/projects/odin/vendor/dompdf/dompdf/src/FrameReflower/Block.php:236
/builds/alpinresorts/dev/projects/odin/vendor/dompdf/dompdf/src/FrameReflower/Block.php:811
/builds/alpinresorts/dev/projects/odin/vendor/dompdf/dompdf/src/FrameDecorator/AbstractFrameDecorator.php:915
/builds/alpinresorts/dev/projects/odin/vendor/dompdf/dompdf/src/FrameReflower/Inline.php:161
/builds/alpinresorts/dev/projects/odin/vendor/dompdf/dompdf/src/FrameDecorator/AbstractFrameDecorator.php:915
/builds/alpinresorts/dev/projects/odin/vendor/dompdf/dompdf/src/FrameReflower/Page.php:141
/builds/alpinresorts/dev/projects/odin/vendor/dompdf/dompdf/src/FrameDecorator/AbstractFrameDecorator.php:915
/builds/alpinresorts/dev/projects/odin/vendor/dompdf/dompdf/src/Dompdf.php:815
/builds/alpinresorts/dev/projects/odin/vendor/nucleos/dompdf-bundle/src/Wrapper/DompdfWrapper.php:63

Not sure what other info would be helpful though. Funny thing is that it's happening in our Gitlab CI pipeline, but locally everything works. I've checked different versions and it looks like last working version was v3.1.0. All the later versions are causing this.

I haven't dig deep to what's is causing it, but if you need more info let me know as it seems we can reproduce it. Since it's a commercial project I can't share the code though.

peter-si avatar Oct 16 '25 10:10 peter-si

An example of the content (HTML + CSS) that's triggering the issue would be the most straightforward mechanism for identifying the cause.

bsweeney avatar Oct 17 '25 13:10 bsweeney

We have it for a base64 png string which is a QR code. I'll try to provide a replication once I'm back in the office.

gigerIT avatar Oct 19 '25 11:10 gigerIT

Can someone with this issue read through #3674? It seems like a possibility that retrieval of the base stylesheet could be failing.

bsweeney avatar Oct 21 '25 17:10 bsweeney

We have a customer that reported this same issue. Error trace:

Fatal error: Uncaught Error: Call to a member function get_content_box() on null
in /home/cslpshop/public_html/wp-content/plugins/woocommerce-pdf-invoices-packing-slips/vendor/strauss/dompdf/dompdf/src/FrameReflower/Block.php:96

Stack trace:
#0 /home/cslpshop/public_html/wp-content/plugins/woocommerce-pdf-invoices-packing-slips/vendor/strauss/dompdf/dompdf/src/FrameReflower/Block.php(236): WPO\IPS\Vendor\Dompdf\FrameReflower\Block->_calculate_width('auto')
#1 /home/cslpshop/public_html/wp-content/plugins/woocommerce-pdf-invoices-packing-slips/vendor/strauss/dompdf/dompdf/src/FrameReflower/Block.php(811): WPO\IPS\Vendor\Dompdf\FrameReflower\Block->_calculate_restricted_width()
#2 /home/cslpshop/public_html/wp-content/plugins/woocommerce-pdf-invoices-packing-slips/vendor/strauss/dompdf/dompdf/src/FrameDecorator/AbstractFrameDecorator.php(915): WPO\IPS\Vendor\Dompdf\FrameReflower\Block->reflow(NULL)
#3 /home/cslpshop/public_html/wp-content/plugins/woocommerce-pdf-invoices-packing-slips/vendor/strauss/dompdf/dompdf/src/FrameReflower/Inline.php(161): WPO\IPS\Vendor\Dompdf\FrameDecorator\AbstractFrameDecorator->reflow(NULL)
#4 /home/cslpshop/public_html/wp-content/plugins/woocommerce-pdf-invoices-packing-slips/vendor/strauss/dompdf/dompdf/src/FrameDecorator/AbstractFrameDecorator.php(915): WPO\IPS\Vendor\Dompdf\FrameReflower\Inline->reflow(NULL)
#5 /home/cslpshop/public_html/wp-content/plugins/woocommerce-pdf-invoices-packing-slips/vendor/strauss/dompdf/dompdf/src/FrameReflower/Page.php(141): WPO\IPS\Vendor\Dompdf\FrameDecorator\AbstractFrameDecorator->reflow()
#6 /home/cslpshop/public_html/wp-content/plugins/woocommerce-pdf-invoices-packing-slips/vendor/strauss/dompdf/dompdf/src/FrameDecorator/AbstractFrameDecorator.php(915): WPO\IPS\Vendor\Dompdf\FrameReflower\Page->reflow(NULL)
#7 /home/cslpshop/public_html/wp-content/plugins/woocommerce-pdf-invoices-packing-slips/vendor/strauss/dompdf/dompdf/src/Dompdf.php(815): WPO\IPS\Vendor\Dompdf\FrameDecorator\AbstractFrameDecorator->reflow()
#8 /home/cslpshop/public_html/wp-content/plugins/woocommerce-pdf-invoices-packing-slips/includes/Makers/PDFMaker.php(63): WPO\IPS\Vendor\Dompdf\Dompdf->render()
#9 /home/cslpshop/public_html/wp-content/plugins/woocommerce-pdf-invoices-packing-slips/includes/Documents/OrderDocument.php(1597): WPO\IPS\Makers\PDFMaker->output()
#10 /home/cslpshop/public_html/wp-content/plugins/woocommerce-pdf-invoices-packing-slips/includes/Documents/OrderDocument.php(1663): WPO\IPS\Documents\OrderDocument->get_pdf()
#11 /home/cslpshop/public_html/wp-content/plugins/woocommerce-pdf-invoices-packing-slips/includes/Main.php(517): WPO\IPS\Documents\OrderDocument->output_pdf('inline')
#12 /home/cslpshop/public_html/wp-includes/class-wp-hook.php(324): WPO\IPS\Main->generate_document_ajax('')
#13 /home/cslpshop/public_html/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters('', Array)
#14 /home/cslpshop/public_html/wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#15 /home/cslpshop/public_html/wp-content/plugins/woocommerce-pdf-invoices-packing-slips/includes/Endpoint.php(88): do_action('wp_ajax_generate_document')
#16 /home/cslpshop/public_html/wp-includes/class-wp-hook.php(324): WPO\IPS\Endpoint->handle_document_requests(Object(WP))
#17 /home/cslpshop/public_html/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array)
#18 /home/cslpshop/public_html/wp-includes/plugin.php(565): WP_Hook->do_action(Array)
#19 /home/cslpshop/public_html/wp-includes/class-wp.php(418): do_action_ref_array('parse_request', Array)
#20 /home/cslpshop/public_html/wp-includes/class-wp.php(818): WP->parse_request('')
#21 /home/cslpshop/public_html/wp-includes/functions.php(1342): WP->main('')
#22 /home/cslpshop/public_html/wp-blog-header.php(16): wp()
#23 /home/cslpshop/public_html/index.php(17): require('/home/cslpshop/public_html/wp-blog-header.php')
#24 {main}

alexmigf avatar Oct 22 '25 09:10 alexmigf

I have the same issue, but the weird thing is it only happens the first time I render a document after installing DomPDF.

After failing, all subsequent document rendering is working without issues.

So it might be related to a cache issue, or as you said, a missing CSS, but it's weird.

I can reproduce the issue with 3.1.1 and 3.1.3, but it works in 3.1.0.

I'm sending an email with stack trace, HTML and CSS to @bsweeney.

Thank you!

bohwaz avatar Oct 24 '25 13:10 bohwaz

I was unable to reproduce the specific error, but this does look like it is caused by the same underlying problem as in 3674. For stylesheets HTTP headers are parsed to look for a valid mime type. In PHP 8.4 or greater we weren't clearing previous headers meaning that a remote file retrieved before render would trigger header inspection on the base stylesheet. The remote file having a non-CSS mime type would cause Dompdf to drop the stylesheet. As a result, Dompdf did not have the core styling needed to render a document correctly.

bsweeney avatar Oct 25 '25 14:10 bsweeney

This is very weird. What my script does is that it downloads the dompdf release ZIP file, unzips it and then calls DomPDF. If I make my script exit after downloading unzipping, and then I reload the page, it works. If I directly call DomPDF just after the unzip, it fails.

I could reduce the code and reliably reproduce the issue with the following code:

dompdf_fail.zip

Here is how I reproduce the issue:

php -S localhost:8081 -t styles &
…
php test.php
PHP Fatal error:  Uncaught Error: Call to a member function get_content_box() on null in /tmp/dompdf/dompdf/vendor/dompdf/dompdf/src/FrameReflower/Block.php:96
Stack trace:
#0 /tmp/dompdf/dompdf/vendor/dompdf/dompdf/src/FrameReflower/Block.php(236): Dompdf\FrameReflower\Block->_calculate_width()
#1 /tmp/dompdf/dompdf/vendor/dompdf/dompdf/src/FrameReflower/Block.php(811): Dompdf\FrameReflower\Block->_calculate_restricted_width()
#2 /tmp/dompdf/dompdf/vendor/dompdf/dompdf/src/FrameDecorator/AbstractFrameDecorator.php(915): Dompdf\FrameReflower\Block->reflow()
#3 /tmp/dompdf/dompdf/vendor/dompdf/dompdf/src/FrameReflower/Inline.php(161): Dompdf\FrameDecorator\AbstractFrameDecorator->reflow()
#4 /tmp/dompdf/dompdf/vendor/dompdf/dompdf/src/FrameDecorator/AbstractFrameDecorator.php(915): Dompdf\FrameReflower\Inline->reflow()
#5 /tmp/dompdf/dompdf/vendor/dompdf/dompdf/src/FrameReflower/Inline.php(161): Dompdf\FrameDecorator\AbstractFrameDecorator->reflow()
#6 /tmp/dompdf/dompdf/vendor/dompdf/dompdf/src/FrameDecorator/AbstractFrameDecorator.php(915): Dompdf\FrameReflower\Inline->reflow()
#7 /tmp/dompdf/dompdf/vendor/dompdf/dompdf/src/FrameReflower/Page.php(141): Dompdf\FrameDecorator\AbstractFrameDecorator->reflow()
#8 /tmp/dompdf/dompdf/vendor/dompdf/dompdf/src/FrameDecorator/AbstractFrameDecorator.php(915): Dompdf\FrameReflower\Page->reflow()
#9 /tmp/dompdf/dompdf/vendor/dompdf/dompdf/src/Dompdf.php(815): Dompdf\FrameDecorator\AbstractFrameDecorator->reflow()
#10 /tmp/test.php(41): Dompdf\Dompdf->render()
#11 /tmp/test.php(46): PDF::toPDF()
#12 {main}
  thrown in /tmp/dompdf/dompdf/vendor/dompdf/dompdf/src/FrameReflower/Block.php on line 96

It works properly if I stop the script after unzip, and then generate the PDF with DomPDF already unzipped.

It works properly if I disable remote loading with $options->set('isRemoteEnabled', false);. It works correctly also if I stop the HTTP server.

So it seems to be a network issue indeed. But I fail to see why it would fail this way.

I wondered if it might be some kind of cache issue, but I don't see what cache could affect this.

bohwaz avatar Oct 27 '25 16:10 bohwaz

It's an issue with the way we're validating stylesheets and is a caching issue of sorts. If the response headers from a HTTP request made before running Dompdf are not cleared manually in PHP 8.4+ then Dompdf is using them as part of the validation. The update I have ready should address the issue and I plan to release that fix this week.

bsweeney avatar Oct 27 '25 16:10 bsweeney

I understand now, thanks.

If I add http_clear_last_response_headers() before loading DomPDF it works!

bohwaz avatar Oct 27 '25 16:10 bohwaz