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()
答案 0 :(得分:0)
如果您使用NReco.PdfGenerator nuget软件包,则以下行是错误的:
Dim pdf As Byte() = Converter.GeneratePdf(sw.ToString(),PdfSharp.PageSize.Letter)
因为t his overload of GeneratePdf method需要两个参数:第一个字符串是主要的HTML内容(必需),第二个字符串用于“ cover”页面(第一页的特殊内容),该字符串是可选的(可以为null)。
要设置输出页面大小,可以设置HtmlToPdfConverter.Size property:
Converter.Size = PageSize.Letter;