While there isn't a built-in solution for this task, it is still achievable with some adjustments. Interestingly, you are the second person to inquire about this and I was able to find a workaround during the first attempt.
The original idea was shared on the dompdf support group.
To begin, a slight modification must be made in the document's stylesheet. Specifically, set the top and bottom page margins to zero to prevent them from affecting the height calculation. In the initial HTML code, include this additional CSS declaration:
@page { margin-top: 0px; margin-bottom: 0px; }
Next, determine the most suitable size for the initial document. For a basic document, an 8cm x 8cm page size was chosen. For more comprehensive content, opt for a taller height to avoid pagination issues. Approximately 226.77pt corresponds to 8cm. Configure the first pass using these dimensions:
$dompdf = new DOMPDF( );
$dompdf->set_paper( array( 0 , 0 , 226.77 , 226.77 ) );
$dompdf->load_html( $first_pass_html );
$dompdf->render( );
Subsequently, determine the total number of pages generated from the initial pass and clear the $dompdf variable for the second round:
$page_count = $dompdf->get_canvas( )->get_page_number( );
unset( $dompdf );
Lastly, render the document once more but adjust the page height based on the calculated value from the first pass multiplied by the number of pages (with added padding for margins).
$dompdf = new DOMPDF( );
$dompdf->set_paper( array( 0 , 0 , 226.77 , 226.77 * $page_count + 20 ) );
$dompdf->load_html( $original_html );
$dompdf->render( );
$dompdf->stream( 'sample.pdf' , array( 'Attachment' => 0 ) );