Andie Andie - 25 days ago 16
HTML Question

MPDF problems with floated div widths

I have a problem that I am struggling to resolve with MPDF6

I have a web application created using codeigniter, which displays results of a questionnaire survey.

I have generated simple stacked bar charts in html and used jquery to animate the bars to their final sizes. Works ok, and no problems.

I want to be able to replicate the results in MPDF, so using the same theory of divs and sizing them accordingly, but this time I am setting the widths as inline style using php, and in most cases I have this working fine, however, if the value = 1, then for some reason mpdf goes funky, see example image below;

Example of MPDF issue

Here is an example of my php markup for the mpdf report

<div class='survey'>
<?php $nw = 390; $w = 400; $tot=0; $totpos=0; $score=0; $cat=0; foreach ($categories as $c) : ?>
<p class='left question'><?php echo $c->category_name; ?></p>
<p class='left perc'><?php echo number_format($summaryAnswers[$cat][6],1); ?>%</p>
<p class='left perc'><?php echo number_format($summaryAnswers[$cat][7],2); ?></p>
<div style='width: <?php echo $w; ?>px;' class='left chart'>
<?php if ($summaryAnswers[$cat][0] > 0) : ?>
<div class='left barchart sagree' style='width: <?php $width = $nw * $summaryAnswers[$cat][0]/$summaryAnswers[$cat][5]; echo $width; ?>px;' tot='<?php echo $summaryAnswers[$cat][5]; ?>' val='<?php echo $summaryAnswers[$cat][0]; ?>' ><span class='label'><?php echo $summaryAnswers[$cat][0]; ?></span></div>
<?php endif; ?>

<?php if ($summaryAnswers[$cat][1] > 0) : ?>
<div class='left barchart agree' style='width: <?php $width = $nw * $summaryAnswers[$cat][1]/$summaryAnswers[$cat][5]; echo $width; ?>px;' tot='<?php echo $summaryAnswers[$cat][5]; ?>' val='<?php echo $summaryAnswers[$cat][1]; ?>' ><span class='label'><?php echo $summaryAnswers[$cat][1]; ?></span></div>
<?php endif; ?>

<?php if ($summaryAnswers[$cat][2] > 0) : ?>
<div class='left barchart neither' style='width: <?php $width = $nw * $summaryAnswers[$cat][2]/$summaryAnswers[$cat][5]; echo $width; ?>px;' tot='<?php echo $summaryAnswers[$cat][5]; ?>' val='<?php echo $summaryAnswers[$cat][2]; ?>' ><span class='label'><?php echo $summaryAnswers[$cat][2]; ?></span></div>
<?php endif; ?>

<?php if ($summaryAnswers[$cat][3] > 0) : ?>
<div class='left barchart disagree' style='width: <?php $width = $nw * $summaryAnswers[$cat][3]/$summaryAnswers[$cat][5]; echo $width; ?>px;' tot='<?php echo $summaryAnswers[$cat][5]; ?>' val='<?php echo $summaryAnswers[$cat][3]; ?>' ><span class='label'><?php echo $summaryAnswers[$cat][3]; ?></span></div>
<?php endif; ?>

<?php if ($summaryAnswers[$cat][4] > 0) : ?>
<div class='left barchart sdisagree' style='width: <?php $width = $nw * $summaryAnswers[$cat][4]/$summaryAnswers[$cat][5]; echo $width; ?>px;' tot='<?php echo $summaryAnswers[$cat][5]; ?>' val='<?php echo $summaryAnswers[$cat][4]; ?>' ><span class='label'><?php echo $summaryAnswers[$cat][4]; ?></span></div>
<?php endif; ?>
</div>


I have tried both px width and %width both options give me the same result...

here is the html that is generated by the script, you can see the values of each width look correct

<div class='survey'>
<p class='left question'>Organisation and Culture</p>
<p class='left perc'>66.7%</p>
<p class='left perc'>2.92</p>
<div style='width: 400px;' class='left chart'>
<div class='left barchart sagree' style='width: 97.5px;' tot='24' val='6' ><span class='label'>6</span></div>

<div class='left barchart agree' style='width: 162.5px;' tot='24' val='10' ><span class='label'>10</span></div>

<div class='left barchart neither' style='width: 32.5px;' tot='24' val='2' ><span class='label'>2</span></div>

<div class='left barchart disagree' style='width: 81.25px;' tot='24' val='5' ><span class='label'>5</span></div>

<div class='left barchart sdisagree' style='width: 16.25px;' tot='24' val='1' ><span class='label'>1</span></div>
</div>


<br class='clear' />


the css..

.survey { font-size: 10pt; }
.survey .question { width: 47%; padding: 5px 0; }
.survey .perc { width: 5%; margin: 0; padding: 5px 0; }
.survey .no { width: 2%; margin: 0; padding: 5px 0; }
.survey .chart { width: 40%; }
.survey .barchart { padding: 5px 0; }
.barchart .label { margin: 0 0 0 5px; }
div.sagree { background: #2DCC00; }
div.agree { background: #F2E930; }
div.neither { background: #888; }
div.disagree{ background: #FFA519; }
div.sdisagree { background: #FF1919; }
.right { float: right; }
.left { float: left; }
.clear { clear: both; }


anyone got any ideas why this is happening?

cheers

Answer

I found a solution which worked for me, through a lot of trial and error...

I created a container div and set the width of the div in % using inline css, then each floated div within the container, was set a width as a % too a bit like this:

<div style='width: 80%'>
    <div style='float: left; width: 20%'>Content</div>
    <div style='float: left; width: 30%'>Content</div>
    <div style='float: left; width: 40%'>Content</div>
    <div style='float: left; width: 10%'>Content</div>
    <div style='float: left; width: 5%'>Content</div>
    <div style='float: left; width: 15%'>Content</div>
    <div style='float: left; width: 40%'>Content</div>
    <div style='float: left; width: 40%'>Content</div>
</div>
Comments