I am trying to save html from the page to pdf using SelectPDF for VB.NET. I pass from webpage json with html to save, get it on server side. It looks like converter converts html successfully (no error thrown), but it breaks on the saving part.
Javascript:
var dataToSend = JSON.stringify({ 'html': $("#content").html() });
$.ajax({
url: "/leaderboards/pdf.aspx",
type: 'POST',
data: dataToSend,
contentType: "application/json; charset=utf-8",
success: function (data) {
$("#dialog").dialog("close");
console.log(data);
},
error: function (errorText) {
console.log(errorText);
}
});
pdf.aspx
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Dim jsonString = New StreamReader(Request.InputStream).ReadToEnd()
Dim jsonObj As JObject = JObject.Parse(jsonString)
Dim html As String = jsonObj.Item("html")
If html.Length > 0 Then
html = "<html><body>" & html & "</body></html>"
' read parameters from the webpage
Dim webPageWidth As Integer = 1024
Dim webPageHeight As Integer = 0
' instantiate a html to pdf converter object
Dim converter As New HtmlToPdf()
' create a new pdf document converting an url
Dim doc As PdfDocument = converter.ConvertHtmlString(html, Request.Url.AbsoluteUri)
' save pdf document
' !!! code breaks here with exception: Unable to evaluate expression.!!!
doc.Save(Response, False, "C:\MyProject\Pdf\Sample.pdf")
' close pdf document
doc.Close()
Else
Response.Write("No Data")
End If
Catch ex As Exception
Response.Write("Error :" + ex.Message)
End Try
End Sub
If I change the line that breaks the code to
doc.Save("C:\MyProject\Pdf\Sample.pdf")
I have empty PDF saved in that location. I also tried to save string with html, but were not successful e.g:
html = "<html><body>hello world</body></html>"
Is it possible to save PDF with this SelectPDF library from the string that represents html? If yes, any pointer why I am getting error "doc.Save(Response, False, "C:\MyProject\Pdf\Sample.pdf")"? Thank you