Render a view into a pdf

Hi ,

I am trying to render a view in my backend and save its html source to a pdf file. For that purpose, I have a pdf function


Function PDF()
Dim opportunities As IEnumerable(Of OpportunityViewModels)
opportunities = TempData(“snapshot”)
If (IsNothing(opportunities)) Then
Return “No Opportunities”
End If
Dim op = opportunities.ToList
Dim s = RenderView(Me, “_LoadTable_Opportunity2”, op)
Dim pdf1 As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf()
Dim sec1 As Aspose.Pdf.Generator.Section = pdf1.Sections.Add()
Dim text1 As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text(s.ToString)
sec1.Paragraphs.Add(text1)
text1.IsHtmlTagSupported = True
pdf1.Save(“C:Somepath\Test.pdf”)
Return True
End Function

and here is the render view function

Function RenderView(controller As OpportunityController, viewName As String, model As Object) As String
ViewData.Model = model
Using sw As New StringWriter()
Dim viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName)
Dim viewContext = New ViewContext(controller.ControllerContext, viewResult.View, ViewData, TempData, sw)
viewResult.View.Render(viewContext, sw)
viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View)
Return sw.GetStringBuilder().ToString()
End Using
End Function

The following code works, however, for some reasons its doesn’t load the proper CSS with the view. In the view page, I called all the necessary CSS files but Aspose doesn’t seem to take them into account.

Please Help,
Thank you




Hi there,


Thanks for your inquiry. Please use new DOM approach to convert HTML to PDF. Please check following documentation links for the details. If you are using some external CSS then you need to pass basepath in HtmlLoadOption object. Hopefully your issue will be resolved. If issue persist then please share your source HTML and CSS, so we will test it at our end and will guide you accordingly.


Please feel free to contact us for any further assistance.

Best Regards,