We have an issue with a static html page when converting it to a pdf-file.
This is a simplified version of the html that we experience the same problem with:
After 42 pages the first page reappears in the pdf - and again at page 82 (pdf attached)
Below I have pasted our code.
Any help in this regard would be greatly appreciated.
Dim filename as String = ""
filename=(request("filename"))
Dim urltoparse as String = ""
urltoparse=(request("urltoparse"))
urltoparse=Replace(urltoparse,"_0OG0_","&")
If InStr(urltoparse,"?")>0 Then
urltoparse=urltoparse+"&tid"+server.urlencode(now())
Else
urltoparse=urltoparse+"?tid"+server.urlencode(now())
End if
Dim margins As String = ""
margins = (request("margins"))
If Not isnumeric(margins) Or Len(margins)=0 Then margins="10"
Dim doc As PdfDocument
doc = New PdfDocument()
doc.PageSettings.SetMargins(CInt(margins))
Dim Orientation As String = ""
Orientation = LCase((request("Orientation")))
If Orientation="landscape" Then
doc.PageSettings.Orientation = PdfPageOrientation.Landscape
else
doc.PageSettings.Orientation = PdfPageOrientation.Portrait
End if
doc.PageSettings.Rotate = CType(System.Enum.Parse(GetType(PdfPageRotateAngle), "RotateAngle0"), PdfPageRotateAngle)
Dim page As PdfPage = Nothing
Dim pageSize As SizeF = SizeF.Empty
Dim convertor As New PdfUnitConvertor()
Dim width As Single = -1
Dim height As Single = -1
page = doc.Pages.Add()
pageSize = page.GetClientSize()
'Calculates the height and width of the pdf image.
width = convertor.ConvertToPixels(page.GetClientSize().Width, PdfGraphicsUnit.Point)
dim html As HtmlConverter = New HtmlConverter()
html.EnableHyperlinks = True
html.AutoDetectPageBreak = True
html.EnableJavaScript = True
Dim result As HtmlToPdfResult = html.Convert(urltoparse, ImageType.Metafile, CInt(Fix(width)), CInt(Fix(height)), AspectRatio.KeepWidth)
If Not result Is Nothing Then
Dim mf As PdfMetafile = New PdfMetafile(TryCast(result.RenderedImage, Metafile))
mf.Quality = 100
Dim format As PdfMetafileLayoutFormat = New PdfMetafileLayoutFormat()
format.Break = PdfLayoutBreakType.FitPage
format.Layout = PdfLayoutType.Paginate
doc.PageSettings.Height = result.RenderedImage.Size.Height
format.SplitTextLines = false
format.SplitImages = false
result.Render(page, format)
Else
Response.Write("Warning ! Please check the HTML link")
End If
Dim rect As RectangleF = New RectangleF(0, 0, doc.Pages(0).GetClientSize().Width, 50)
Dim footer As PdfPageTemplateElement = New PdfPageTemplateElement(rect)
Dim font As PdfStandardFont = New PdfStandardFont(PdfFontFamily.Helvetica, 10.5F)
Dim brush As PdfSolidBrush = New PdfSolidBrush(Color.Black)
'Create page number field
Dim pageNumber As PdfPageNumberField = New PdfPageNumberField(font, brush)
'Create page count field
Dim count As PdfPageCountField = New PdfPageCountField(font, brush)
'Add the fields in composite fields
Dim compositeField As PdfCompositeField = New PdfCompositeField(font, brush, "Side {0} af {1}", pageNumber, count)
compositeField.Bounds = footer.Bounds
'Draw the composite field in footer
compositeField.Draw(footer.Graphics, New PointF(470, 40))
'Add the footer template at the bottom
doc.Template.Bottom = footer
doc.Save(filename, Response, HttpReadType.Save)