DomPDF Calculate height of each div to prevent multiple pages - php

I am currently creating a resume exporting tool using domPDF. I have a DB that contains all the resume data (title, description). With a loop, I am building my PDF resume which looks like this :
This is the code for the left column of my resume :
$q_cv_categories = $bdd->query('SELECT * FROM cv_category WHERE cv_column = "left"');
$q_cv_categories->execute();
while($cv_categories = $q_cv_categories->fetch()){
$html .= '<div class="special-heading-pdf">';
$html .= '<h3>'.constant($cv_categories['category_name']).'</h3>';
$html .= '</div>';
$q_cv_data = $bdd->prepare('SELECT * FROM cv_data WHERE category_id = :category ORDER BY importance DESC LIMIT 5');
$q_cv_data->bindValue('category', $cv_categories['id'], PDO::PARAM_INT);
$q_cv_data->execute();
while($cv_data = $q_cv_data->fetch()){
$html .= '<div class="resume-information"><h4 class="title-resume-data">'.constant($cv_data['title']).'</h4>';
$html .= '<p class="description-resume-data">'.constant($cv_data['description']).'</p></div>';
}
$q_cv_data->closeCursor();
}
$q_cv_categories->closeCursor();
I would like to be able to calculate the render height of each div with the resume-information class, so I can display 4 instead of 5 computer science achievements (or even 3 if needed) in order to keep the resume one page only.
Thanks

I came up with the idea of calculating the exact height of each element in the resume. For example a line of descriptive text has a height of 17px in my PDF (A4, height of 1122px).
For the right column of the resume, a line of text has a mean of 78 characters. By doing a strlen() of the description, I can approximate quite precisely the number of lines it will be taking.
Here is the example of the script I used to calculate the height of the right column :
$q_cv_categories = $bdd->query('SELECT * FROM cv_category WHERE cv_column = "right"');
$q_cv_categories->execute();
while($cv_categories = $q_cv_categories->fetch()){
$right_height += CFG_RESUME_PDF_CSS_MARGIN_TOP_HEADING; // margin-top of heading computer achievement, work exp, etc.
$right_height += CFG_RESUME_PDF_LINE_HEIGHT_PX*GetResumeLineCount(constant($cv_categories['category_name']), "right", "heading"); // heading
$right_height += CFG_RESUME_PDF_CSS_PADDING_HEADING_BLUELINE+CFG_RESUME_PDF_CSS_HEADING_GREYLINE+CFG_RESUME_PDF_CSS_HEADING_BLUELINE; // blue line of heading
$right_height += CFG_RESUME_PDF_CSS_MARGIN_BOTTOM_HEADING; // margin-bottom of heading
$q_cv_data = $bdd->prepare('SELECT * FROM cv_data WHERE category_id = :category ORDER BY importance DESC LIMIT 5');
$q_cv_data->bindValue('category', $cv_categories['id'], PDO::PARAM_INT);
$q_cv_data->execute();
while($cv_data = $q_cv_data->fetch()){
$right_height += CFG_RESUME_PDF_LINE_HEIGHT_PX*GetResumeLineCount(constant($cv_data['title']), "right", "title");
$right_height += CFG_RESUME_PDF_LINE_HEIGHT_PX*GetResumeLineCount(constant($cv_data['description']), "right", "description");
$right_height += CFG_RESUME_PDF_CSS_MARGIN_BOTTOM_RESUME_INFORMATION; // margin-bottom of resume-information
}
$q_cv_data->closeCursor();
}
$q_cv_categories->closeCursor();
The last step is to display for example 4 instead of 5 computer science achievements if the total height is more than 1100px (with a margin left of 22px), then calculate the total height once more, etc.

Related

FPDF- How to adjust other multicells if one multicell is affected?

I'm trying to generate a table in fpdf with dynamic content in it. The problem is in the line $pdf->MultiCell(50,10,$d2['shade'],'LRB',1);. This cell has a lot of
content in it. So when the height of this cell is increased, the height of other cells remains the same. How do I adjust it accordingly?
$sql3 = "SELECT `materialDesc`,`chart`,`materialSize`,`remarks`, GROUP_CONCAT(shade SEPARATOR ',')shade, GROUP_CONCAT(shade SEPARATOR ',')shade SUM(quantity)quantity FROM `" . $DB->pre . "order_detail` WHERE `orderID`= '$orderID' GROUP BY `materialSize`, `materialDesc`,`chart`,`remarks` ";
$rows3 = $DB->dbRows($sql3);
if ($DB->numRows > 0){
$pdf->SetLineWidth(0);
$pdf->SetFont('Arial','',10);
$newL=90; ////fixed cell spaced in y-axis for quantity
foreach ($rows3 as $d2) {
$pdf->SetXY(11,$newL);
$pdf->MultiCell(30,10,$d2['materialDesc'],'LRB',1);
$pdf->SetXY(41,$newL);
$pdf->MultiCell(13,10,$d2['chart'],'LRB',1);
$pdf->SetXY(54,$newL);
$pdf->MultiCell(50,10,$d2['shade'],'LRB',1); /////line with the problem/////
$pdf->SetXY(104,$newL);
$pdf->MultiCell(13,10,$d2['materialSize'],'LRB',1);
$pdf->SetXY(117,$newL);
$pdf->MultiCell(12,10,$d2['quantity'],'LRB',1);
$pdf->SetXY(129,$newL);
$pdf->MultiCell(15,10,'ROL','LRB',1);
$pdf->SetXY(144,$newL);
$pdf->MultiCell(18,10,'OPEN','LRB',1);
$pdf->SetXY(162,$newL);
$pdf->MultiCell(35,10,$d2['remarks'],'LRB',1);
$newL += 7;
}
}
$pdf-> Ln();
You can find the NbLines() method in this script, which allows you to pre-calculated the needed lines of a multicell.
With this information you can use MultiCell throughout but you have to draw the borders your own (as you also can see in the link above).

Adding a break to product title on shop pages for a certain length

I am trying to get more of a consistent grid layout on my shop loop.
The product title spans over 1 or 2 lines depending on the string length, therefore if the string length is below the amount which forces it to overlap to the next line I want to add a break '' so that it doesn't affect the overall spacing of the shop loop page/
This is the code I have tried at the moment:
<?php
echo "test";
$title = get_the_title();
if ( strlen($title) < 29 )
{
echo '<br>';
}
?>
I have put it in content-product.php woocommerce template, but it's not working.
Is my code correct?
This answer is based on both "title length" an "words length", to avoid breaking a word.
This function partially based on this answer and on woocommerce_template_loop_product_title() WooCommerce native function, that is used on content-product.php WooCommerce template, to display the title in Shop pages.
Here I have included your limit string length, but it's also based on a complex "words length" detection, to avoid break words:
if ( ! function_exists( 'woocommerce_template_loop_product_title' ) ) {
// Show the product title in the product loop. By default this is an <h3> html tag.
function woocommerce_template_loop_product_title() {
// Define the lenght limit for title (by line)
$limit = 29;
$title = get_the_title();
$lenght = strlen($title);
// 1. The title length is higher than limit
if ( $lenght >= $limit ) {
$title_arr1 = array();
$title_arr2 = array();
$sum_length_words = -1;
// an array of the words of the title
$title_word_arr = explode( ' ', $title );
// iterate each word in the title
foreach( $title_word_arr as $word ){
// Length of current word (+1 space)
$length_word = strlen($word) + 1;
// Adding the current word lenght to total words lenght
$sum_length_words += $length_word;
// Separating title in 2 arrays of words depending on lenght limit
if ( $sum_length_words <= $limit )
$title_arr1[] .= $word;
else
$title_arr2[] .= $word;
}
// Converting each array in a string
$splitted_title = implode(" ", $title_arr1). ' ('. strlen(implode(" ", $title_arr1)) .')';
$splitted_title .= '<br>'; // adding <br> between the 2 string
$splitted_title .= implode(" ", $title_arr2). ' ('. strlen(implode(" ", $title_arr2)) .')';
echo '<h3>' . $splitted_title . '</h3>';
// 2. The title length is NOT higher than limit
} else {
echo '<h3>' . $title . '</h3>';
}
}
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.
I think this is not a correct way in order to place a tag in between product title.
I think you just need to fix the amount of characters used in your product title for displaying in product page like for example if
product title is like "Test Bank for Medical Terminology: A Living Language, 5/E"
in this case you can limit the title as "Test Bank for Medical Terminology..." depending on your layout
so in this way, your layout will remain same and good
What will i suggest is using the following
h1{ height: 40px; width: 250px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;}
<h1>Sample test test test test</h1>
This way you can easily display the titles always in one line. If that doesn't work and you want to display the full title, then you can use some tooltip plugin that will display the full title on hover

How to get the offset of a text in HTML tag and all the parents to the root tag in PHP?

I extract from an article for example the publicationYear, title and authors like this:
$aut = $xpath->query("//table[#cellpadding='6']//b[1]");
$authors = array();
foreach($aut as $node)
$authors[] = $node->nodeValue;
$title = $doc->getElementsByTagName('h3')->item(1);
$publicationYear = $xpath->query("//p[1]//text()[(following::br)]")->item(0)->nodeValue;
$aux = $xpath->query("//p[2]//text()[(preceding::br)]");
$doi = substr($aux->item($aux->length - 1)->nodeValue, 4);
For all strings(the full name, year, title) i need to get even all the tags that come before like :
form1_table3_tbody1_tr1_td1_table5_tbody1_tr1_td2_p2
and the position in the tag like start: 163,end: 190.
I know only that those informations are grouped in certain tags, but i need to get even the index of the tag if it has siblings that's why the example has table 3 for the third son of forum 1.
If there's a way of doing it in php or at least javascript
UPDATE
In te article I have:
...
<td valign="top">
<h3 class="blue-space">D-Lib Magazine</h3>
<p class="blue">November/December 2014<br>
Volume 20, Number 11/12<br>Table of Contents
</p>
...
and the $publicationYear from the first code get this val 2014.
The first code works fine.
I need to create other 3 variables like $fathers =...td1_p1,
$start=18, $end=22

PHP MySql Progress Bar [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Progress bar layout using CSS and HTML
I'm making a game that involves energy, EXP, level, missions, etc. Well, When you do missions it costs energy and you recieve cash and EXP once you do a mission. Well, it requires EXP to level up. Leveling up works, but I'm wanting to add a progress bar to show how close you are to level up. An example is Exp: 26,175/27,100. Each time you level the max_exp raises up 250, but was wanting a bar behind the text to show how close they are to level up instead of just seeing text. Here's the leveling coding below..
<?php
if ($exp >= $max_exp)
{
$sql = "UPDATE users SET level=(level + 1) , max_exp=(max_exp + 250) WHERE id='".$id."' LIMIT 1";
$res = mysql_query($sql);
if ($exp >= $max_exp)
echo '';
}
else
{
}
?>
You can echo your ratio as inline css.
An example:
css:
div#value
{
display: block;
z-index: 2;
height: 10px;
}
php/html:
$value = 35;
//this line will give you "width: 35%"
<div id="value" style="width:<?php echo $value; ?>%;">
It's just an HTML/CSS issue: make a table with just one row and TWO column (say one fullgreen, one white), then move the width ratio of the two columns according to your value

DIV Auto scrolling

$sql = mysql_query('SELECT * FROM mdl_course c');
foreach($sql as $ss)
{
$coursename = $ss->id;
$summary = $ss->summary;
echo '<div style="min-height:300px;">
<div style="min-height:100px,overflow:auto;overflow-y:scroll;">'.$summary.'
</div>
</div>';
}
Since I have more information in summary field from database and that too in html format, The summary displays out of the min-height.
I want the summary to be displayed within that height of div and overflow if it has more summary. Could anyone help me
?
You should use height as opposed to min-height, since if the height is more than 100px, it will just get larger, rather than scroll
So using height ... if the height came to 150px, you'll be able to scroll 50px down (if you see what I mean).

Categories