3

(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:

  • $html code I want to print (string);
  • $css code, with full details of the font and size of all items that appear in the HTML (string);
  • all relevant browser settings (fonts, sizes, etc.);
  • page $width, in pixels (int).

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.

Community
  • 1
  • 1
Erel Segal-Halevi
  • 33,955
  • 36
  • 114
  • 183
  • Wouldn't it be simpler to use jQuery? – Meisam Mulla Jul 26 '12 at 04:59
  • I need to do this on the server side, 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. – Erel Segal-Halevi Jul 26 '12 at 05:46
  • @ErelSegalHalevi You don't need it to be server-side. Just get height in JavaScript and send AJAX request to a PHP script with information about height. – Nikola K. Jul 26 '12 at 10:38
  • OK, but by then, the user has already seen the content... I may need more than one iteration - put the content in a div with width 800px and measure the height, then try a width of 600px and measure the height, etc., until I find the best width. If I use a client-side measure, the user will see the content flashing in each iteration. I need it to be server side so that the user only sees the final result. – Erel Segal-Halevi Jul 27 '12 at 05:49

3 Answers3

0

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).

Community
  • 1
  • 1
0

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.

-1

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

Community
  • 1
  • 1
uzyn
  • 6,625
  • 5
  • 22
  • 41
  • 1
    "PHP cannot determine HTML element height from server-side" - of course PHP cannot access the HTML rendering directly, but there must be a way to do the calculation indirectly. After all, this is deterministic - all the data is there. – Erel Segal-Halevi Jul 26 '12 at 05:48
  • What data is there? Only HTML tags in text form is available at server-side. – uzyn Jul 26 '12 at 05:48
  • The Javascript solution is not enough as it only calculates the height *after* it has been displayed, and I need to know the height *before* I display it, see my edit. – Erel Segal-Halevi Jul 26 '12 at 05:48
  • That is not possible. You have to do it at client-side, and maybe feed it back via AJAX. – uzyn Jul 26 '12 at 05:50
  • "Only HTML tags in text form" - I also have the full CSS. So, just like the browser (which is a computer program) can take HTML and CSS and decide how to display it, so does the PHP program. – Erel Segal-Halevi Jul 26 '12 at 05:51
  • It **is not** possible. HTML and CSS are only texts to PHP. It's the rendering engine (of client's browser) that's rendering it. That being said, it should also be obvious that height of an HTML element is not identical from browsers to browsers. – uzyn Jul 26 '12 at 05:54
  • The data is not all there when the text is on the server. How the text is displayed depends on the user's specific browser and OS. Even if you include special handling for every browser/OS, you still don't know what the end user has their font size set to, etc. – octern Jul 26 '12 at 05:55
  • "It's the rendering engine (of client's browser) that's rendering it" - OK, and the rendering engine is written some programming language (C++, for example)? So, it is possible to write a similar program in PHP. – Erel Segal-Halevi Jul 27 '12 at 05:53
  • Also, for the sake of this question, assume that all browser settings are available. – Erel Segal-Halevi Jul 27 '12 at 05:54