【问题标题】:DomPDF Calculate height of each div to prevent multiple pagesDomPDF 计算每个div的高度,防止出现多页
【发布时间】:2020-04-22 18:31:02
【问题描述】:

我目前正在使用 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 类计算每个 div 的渲染高度,这样我可以在为了让简历只保留一页。

谢谢

【问题讨论】:

    标签: php html dompdf


    【解决方案1】:

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

    对于简历的右栏,一行文字的平均长度为 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 个计算机科学成就,然后再次计算总高度,等等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-08
      • 1970-01-01
      • 1970-01-01
      • 2015-09-24
      • 2016-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多