【问题标题】:Page Header overlaping content when using Rotativa to generate PDF from HTML使用 Rotativa 从 HTML 生成 PDF 时页眉重叠内容
【发布时间】:2020-07-04 15:56:59
【问题描述】:

我正在尝试使用“Rotativa”库将HTML file 转换为 PDF,但是在使用自定义标题时我遇到了一些问题,这是我的代码:

string customSwitches = string.Format("--header-html  \"{0}\" " +
                               "--header-spacing \"0\" " +
                               "--footer-html \"{1}\" " +
                               "--footer-spacing \"10\" " +
                               "--footer-font-size \"10\" " +
                               "--header-font-size \"10\" ", header, footer);

            var invoice = new ViewAsPdf("Index", _invoiceRepository.GetInvoice(1))
            {
                FileName = "MyPDF.pdf",
                CustomSwitches = customSwitches,
                PageMargins = margin,
                PageSize = size,
                PageOrientation = orientation                
            };

            return invoice;

页眉和页脚已按预期成功注入到每个页面的顶部和底部,但问题是页眉与其余内容重叠。 如何为页眉预留空间?

【问题讨论】:

    标签: asp.net asp.net-mvc rotativa


    【解决方案1】:

    我在设置一次--header-spacing \"0\" 时遇到了类似的问题。所以对我有用的解决方案是在标题和内容之间设置更大的间距。

    例如,请尝试将间距设置为 7(以毫米为单位,您可以在 WkHtmlToPdf documentation 中查看)。这样,您可以使用之前生成的 PDF 检查当前 PDF,并验证更改是否解决了问题。到达那里后,您应该可以将度量调整为适合您的值。

    因此,您的代码应如下所示:

    string customSwitches = string.Format("--header-html  \"{0}\" " +
                               "--header-spacing \"7\" " +
                               "--footer-html \"{1}\" " +
                               "--footer-spacing \"10\" " +
                               "--footer-font-size \"10\" " +
                               "--header-font-size \"10\" ", header, footer);
    

    我在我的一个项目中尝试了您的代码,似乎运行良好。

    希望对您有所帮助。如果问题仍然存在,在您的 HTML 标题和 PDF 正文内容中包含一些示例应该很有用,以检查具体情况。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-24
      • 1970-01-01
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      • 2017-11-30
      • 2013-10-14
      • 1970-01-01
      相关资源
      最近更新 更多