使用 nreco.generated 将 HTML 转换为 PDF 时第一页空白

Blank 1st page when converting HTML to PDF using nreco.generated

Nreco 成功地将我的 html 代码转换为 pdf,但它在 pdf 的第一页创建了空白页,A4 页面大小为 5,信纸大小为 104。

我搜索了这个问题并测试了以下所有样式,但都不起作用。

page-break-before:avoid;
page-break-after:avoid;
page-break-inside :avoid;



Dim sw As New StringWriter()
Dim htw As New HtmlTextWriter(sw)
Panel1.RenderControl(htw)
Dim Converter As HtmlToPdfConverter = New HtmlToPdfConverter()
Dim htmlContent As String = sw.ToString()
Dim pdf As Byte() = Converter.GeneratePdf(sw.ToString(),PdfSharp.PageSize.Letter)
Response.Charset = "utf-8"
Response.ContentEncoding = System.Text.Encoding.UTF8
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=" + "test.pdf")
Response.Buffer = True
Response.BinaryWrite(pdf)
Response.Flush()
Response.Clear()
Response.End()

如果您使用 NReco.PdfGenerator nuget 包,则以下行是错误的:

Dim pdf As Byte() = Converter.GeneratePdf(sw.ToString(),PdfSharp.PageSize.Letter)

因为 this overload of GeneratePdf method 需要 2 个参数:第一个字符串是主要 HTML 内容(必需),第二个字符串是 'cover' 页面(第一页的特殊内容)是可选的(可以为空)。

要设置输出页面大小,您可以设置 HtmlToPdfConverter.Size property:

Converter.Size = PageSize.Letter;