selectPDF不会从HTML字符串中保存PDF

时间:2016-03-31 13:31:39

标签: c# .net vb.net pdf

我正在尝试使用SelectPDF for VB.NET将html从页面保存到pdf。 我从网页json传递html来保存,在服务器端获取它。它看起来像转换器成功转换html(没有抛出错误),但它在保存部分中断。

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

如果我将破坏代码的行更改为

doc.Save("C:\MyProject\Pdf\Sample.pdf")

我在该位置保存了空PDF。我也尝试用html保存字符串,但没有成功,例如:

 html = "<html><body>hello world</body></html>"

是否可以使用此SelectPDF库从代表html的字符串中保存PDF?如果是的话,任何指针我为什么会收到错误&#34; doc.Save(Response,False,&#34; C:\ MyProject \ Pdf \ Sample.pdf&#34;)&#34;?谢谢

1 个答案:

答案 0 :(得分:2)

如果只需要将pdf文档保存在磁盘上,则不应使用方法调用doc.Save(Response,False,“C:\ MyProject \ Pdf \ Sample.pdf”)。 doc.Save(Response,False,“Sample.pdf”)的目的是将PDF发送到浏览器并建议下载名称(Sample.pdf - no path)。

要将PDF保存在磁盘上,只需使用doc.Save(“C:\ MyProject \ Pdf \ Sample.pdf”)。

运行一个简单的测试并确保它正常工作:

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

在您确定使用简单的html转换运行正常(应该不是问题)之后,请检查您要发送到转换方法ConvertHtmlString的html和baseUrl。将它们记录到文件中。看看它们是否符合你的期望。

由于您使用的是javascript,可能需要一些时间才能加载,因此请尝试在转换前输入延迟: http://selectpdf.com/docs/ConversionDelay.htm

会是这样的:

' specify the number of seconds the conversion is delayed
converter.Options.MinPageLoadTime = 2