DomPDF 计算每个 div 的高度以防止多页

DomPDF Calculate height of each div to prevent multiple pages

我目前正在使用 domPDF 创建一个简历导出工具。我有一个包含所有简历数据(标题、描述)的数据库。通过循环,我正在构建我的 PDF 简历,如下所示:

这是我简历左栏的代码:

$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();

我希望能够使用 resume-information class 计算每个 div 的渲染高度,这样我就可以显示4 项而不是 5 项计算机科学成就(如果需要,甚至 3 项),以便让简历只占一页。

谢谢

我想出了计算简历中每个元素的确切高度的想法。例如一行描述性文字在我的PDF(A4,高度1122px)中的高度为17px。

对于简历的右栏,一行文本的平均长度为 78 个字符。通过执行描述的 strlen(),我可以非常精确地估计它将占用的行数。

这是我用来计算右栏高度的脚本示例:

$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();

最后一步是如果总高度超过1100px(留有22px的边距),则显示例如4个而不是5个计算机科学成就,然后再次计算总高度等