wkhtmltopdf - wkhtmltopdf中页面顶部的标题高度和定位标题

标签 wkhtmltopdf

在我的应用程序中,允许用户定义自己的打印页眉和页边距。
对于标题,用户可以提供以下两个字段:

  • (a) 以毫米为单位的页面顶部边距(从页面顶部开始)
  • (b) 以毫米为单位的页眉边距(从页面顶部)

  • 字段(a)和(b)如下图所示:
    Margins depiction

    例如,如果用户提供 (a) 为 10 毫米和 (b) 为 100 毫米,则“标题区域”的高度将为 90 毫米。

    现在标题中的内容需要在标题区域的顶部对齐。

    有没有办法设置标题区域高度并在此标题区域内顶部对齐内容。

    目前,我正在为 做以下工作标题-html 选项:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8" />
    
        <style type="text/css">
            body 
            {
                border:0; 
                margin: 0;
                padding: 0;
                height: 90mm;  //in actual program, this is set dynamically in PHP
            }
        </style>
    
        <script type="text/javascript">
        function subst() 
        {
            //process the GET parameters and accordingly set the Header Region text info
        }  
        </script>
    </head>
    
    <body onload="subst()">
        <div>
            //text
        </div>
    </body>  
    </html>
    

    margin 最高选项设置为 100mm。

    我曾尝试为 body CSS 设置 vertical-align: top,但这也不起作用。

    我实现的最终输出似乎没有在顶部或中间垂直对齐标题区域内容。它位于标题区域中间稍上方的某个位置。

    最佳答案

    编辑 :似乎只有在基于 Windows 的系统中安装 wkhtmltopdf 时才需要此修复程序。

    经过大量试验和错误,我观察到以下逻辑似乎有效:

  • 的body元素html-head 需要将高度设置为 Header Region 实际高度的 1.33 倍。 (为什么?我不知道。这可能是由于某些 QT 或 wkhtmltopdf 渲染方式造成的,但它实际上适用于所有情况,无论标题区域内容中的行如何)。
  • 正文的直接(也是唯一的)子元素设置为 100% 高度并 overflow hidden 的内容

  • 因此,更改后的 html-head 代码(对于上述示例)如下所示:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8" />
    
        <style type="text/css">
            body 
            {
                border:0; 
                margin: 0;
                padding: 0;
                height: 120mm;
                /*
                In the actual program this height is calculated dynamically in PHP as
                ($headerMargin - $topMargin) * 1.33
                */
            }
    
            #master-div
            {
                overflow: hidden;
                height: 100%;
            }
    
        </style>
    
        <script type="text/javascript">
        function subst() 
        {
            //process the GET parameters and accordingly set the Header Region text info
        }  
        </script>
    </head>
    
    <body onload="subst()">
        <div id="master-div">
            //The actual content goes here
        </div>
    </body>  
    </html>
    

    我已经检查了至少 7 到 8 种不同的纸张尺寸(包括设置自定义页面宽度和页面高度。还检查了不同的页眉区域内容大小(从 1 行到 20 行)。

    唯一的问题是,这个 1.33 倍标题区域高度的逻辑似乎只有在 Header Margin(b)/Footer Margin 小于或等于纸张高度的三分之一时才有效。如果边距高度超过该值,则内容会随着边距高度的增加而被剪裁。但我想,通常不需要如此大的标题高度。

    关于wkhtmltopdf - wkhtmltopdf中页面顶部的标题高度和定位标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15299869/

    相关文章:

    asp.net-mvc - 如何将生成的 HTML 存储为字符串,而不是使用 Razor View 引擎写入浏览器?

    linux - 无法从 cronjob 运行 wkhtmltopdf

    html - wkhtmltopdf 缺少 SVG 路径(渲染)

    javascript - 在 wkhtmltopdf 进程中运行脚本

    c# - 使用 WKHTMLTOPDF 以 HTML 格式显示图像,但不以 PDF 格式显示图像

    ruby-on-rails - wicked_pdf_image_tag 在查找图像时解析为公共(public)文件夹

    php - 打印机使用的 PDF 中的标记,如何生成和 QA 过程

    php - WKHTMLTOPDF -- 是否可以显示动态标题?

    python-3.x - Ubuntu 中的 Azure Pipeline 部署到 Debian 中的 Azure 应用服务

    ubuntu - 更新后 Wkhtmltopdf 字体大小增加