使用 AbcPdf 将 HTML 转换为 PDF 时的分页符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11291447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:23:30  来源:igfitidea点击:

Page break when HTML to PDF with AbcPdf

htmlpdfabcpdf

提问by Remo H. Jansen

I'm trying to create a report in PDF with abcPdf. Everything works but I would like to add a page number and margin at the bottom of each page as well as avoid cuts in the middle of a row as you can see in the picture:

我正在尝试使用 abcPdf 以 PDF 格式创建报告。一切正常,但我想在每页底部添加页码和边距,并避免在行中间进行切割,如图所示:

page break

分页符

var theDoc = new Doc { TopDown = true };
var pageRef = theDoc.AddImageUrl(pdfUrl, true, 1903, true);
while (theDoc.Chainable(pageRef))
{
    theDoc.Page = theDoc.AddPage();
    //I guessI have to do something here???
    pageRef = theDoc.AddImageToChain(pageRef);
}

Does somebody know if it is possible?

有人知道这是否可能吗?

回答by Remo H. Jansen

It did work but I think AbcPdf is using the HTML rendering of IE so the best thing you can do is to manually set the rendering engine to be gecko (Dont forget that you need and extra DLL) or to update IE in your web server.

它确实有效,但我认为 AbcPdf 正在使用 IE 的 HTML 渲染,因此您可以做的最好的事情是手动将渲染引擎设置为 Gecko(不要忘记您需要额外的 DLL)或在您的 Web 服务器中更新 IE。

theDoc.HtmlOptions.Engine = EngineType.Gecko; 

Then to add a page break just use

然后添加分页符只需使用

<div style="page-break-before:always">&nbsp;</div> 

Thanks to feeela for the comment.

感谢feeela 的评论。

回答by OnceUponATimeInTheWest

ABCpdf includes two HTML rendering engines.

ABCpdf 包括两个 HTML 渲染引擎。

The MSHTML one is based around Trident and will produce output broadly similar to the version of IE installed on your system.

MSHTML 是基于 Trident 的,将产生与您系统上安装的 IE 版本大致相似的输出。

The Gecko one is based around Firefox and as of June 2013 will produce output broadly similar to that you see in Firefox 21.

Gecko one 基于 Firefox,截至 2013 年 6 月,将产生与您在 Firefox 21 中看到的大致相似的输出。

You can switch between the two engines using the Doc.HtmlOptions.Engine property.

您可以使用 Doc.HtmlOptions.Engine 属性在两个引擎之间切换。

Both engines support the page-break CSS styles. You can use the following:

两个引擎都支持分页 CSS 样式。您可以使用以下内容:

<div style="page-break-before:always">some text</div>
<div style="page-break-after:always">some text</div> 
<div style="page-break-inside:avoid">some text</div> 

Note that the page-break-inside is an addition to the base MSHTML behavior.

请注意,page-break-inside 是对基本 MSHTML 行为的补充。

The two engines treat these constructs slightly differently. In general MSHTML is more forgiving and intuitive. Howwever the element to which the style is applied must be visible for it to work.

两个引擎对这些结构的处理略有不同。一般而言,MSHTML 更加宽容和直观。但是,应用样式的元素必须可见才能工作。

As such, if the styles don't produce breaks in the places you would expect, the easiest way to debug them is to apply a border style to the element so you can see where the break should occur. This normally makes the cause of the problem obvious.

因此,如果样式没有在您期望的位置产生中断,调试它们的最简单方法是将边框样式应用到元素,以便您可以看到应该发生中断的位置。这通常会使问题的原因显而易见。

The page break styles in the Gecko engine are not always applied as intuitively as they are in MSHTML. The root of this is the CSS specification that which says that break styles must be appliable to block-level elements within the "normal flow of the root element". It allows for these styles to be applied to other elements but does not mandate it.

Gecko 引擎中的分页样式并不总是像在 MSHTML 中那样直观地应用。其根源是 CSS 规范,该规范规定中断样式必须适用于“根元素的正常流”中的块级元素。它允许将这些样式应用于其他元素,但并未强制执行。

The upshot of this, within the Gecko engine, is that page break styles cannot be applied within tables, to elements such as table rows. If you are unsure about whether something is likely to work just try Print Preview from within Firefox 21.0 as a simple sanity check.

这在 Gecko 引擎中的结果是,分页样式不能在表格中应用到诸如表格行之类的元素。如果您不确定某些东西是否可行,只需在 Firefox 21.0 中尝试打印预览作为简单的健全性检查。

回答by Has AlTaiar

There is a better way for fixing this. Instead of putting this <div />to force Page breaking, you could put a CSSattribute on the container of your html (ie <div />, <table />, etc). In the css set the page-break-insideto auto.

有一个更好的方法来解决这个问题。而不是把如此<div />给力破页,你可以把一个CSS属性HTML的容器上(即<div /><table />等)。在CSS设置page-break-insideauto

// in CSS
#ContainerID
{
    page-break-inside: auto;
}

回答by KillerKiwi

Yeah I've dealt with this issue we solved it by adding a row to the table and seeing if it would still fit on the page by checking the height of the html vs the rectangle.

是的,我已经解决了这个问题,我们通过在表格中添加一行并通过检查 html 与矩形的高度来查看它是否仍然适合页面来解决它。

It works nicely once all setup but is a little slower

一旦所有设置完成,它就可以很好地工作,但速度有点慢