3

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

1 Answer 1

2

The method call doc.Save(Response, False, "C:\MyProject\Pdf\Sample.pdf") should not be used if you need to just save the pdf document on the disk. The purpose of doc.Save(Response, False, "Sample.pdf") is to send the PDF to the browser and suggest a download name (Sample.pdf - no path).

To save the PDF on disk, simply use doc.Save("C:\MyProject\Pdf\Sample.pdf").

Run a simple test and make sure it works fine:

Dim html as String = "<html><body>hello world</body></html>"
Dim doc As PdfDocument = converter.ConvertHtmlString(html, "")
doc.Save("C:\MyProject\Pdf\Sample.pdf")

After you are sure the conversion runs fine with simple html (should not be a problem), check to see what html and baseUrl you are sending to the conversion method ConvertHtmlString. Log them into a file. See if they are what you expect.

Since you are using javascript, that might take some time to load, so try to enter a delay before the conversion: http://selectpdf.com/docs/ConversionDelay.htm

It will be something like this:

' specify the number of seconds the conversion is delayed
converter.Options.MinPageLoadTime = 2
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much! Adding converter.Options.MinPageLoadTime = 2 helped!
How could I convert this PDF file to byte array instead of saving into a physical file. Kindly assist me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.