(Following this question: html columns whose width automatically changes according to their content )
From a PHP script, I print some HTML code, and I would like to know what will be the height of the result (for paging calculation). Suppose I have the following input:
I create a page that looks like this:
<html>
<head>
<style>$css</style>
</head>
<body style='width:$width'>
$html
</body>
</html>
I want to know, from within the PHP script, the number of pixels from the top of the page to the bottom of the text.
EDIT: I need to do this on the server side, before the content is displayed, because I use this to calculate what content to put. For example, if the content is too high, I might decide to put only the first half, etc.
Another possible use is to calculate what width is needed to achieve a particular height. For example, I might try to put it in a div with width 200px, but then the result may be too high; then I try width 400px, etc., until I find the width the gives the height that I want.
HTML rendering is done at client-side while PHP resides at server.
As such, PHP cannot determine HTML element height from server-side.
You would, however, be able to obtain that using JavaScript.
See: Get full height of a clipped DIV
If you want to create your own server-side browser, you will have problems with load time. And if this is not a big problem, you can parse css (something like this: link) to find out what paddings, margins, fonts, line-heights and so on used in your $html
variable, then, if you know font specifics, you can try to calculate content height (naturally given, from what browser the user entered).
You might be able to use the TCPDF class to do this. According to this thread the class has limit support for CSS but if your styles are supported the idea would be to examine the current positions, (GetX() and GetY()) before and after a writeHTML method call.