【问题标题】:selectPDF doesnt save PDF from HTML stringselectPDF 不会从 HTML 字符串中保存 PDF
【发布时间】:2016-07-19 23:58:24
【问题描述】:

我正在尝试使用 SelectPDF for VB.NET 将页面中的 html 保存为 pdf。 我从带有html的网页json传递来保存,在服务器端获取它。看起来转换器成功转换了 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?如果是,任何指针为什么我会收到错误“doc.Save(Response,False,”C:\MyProject\Pdf\Sample.pdf“)”?谢谢

【问题讨论】:

    标签: c# .net vb.net pdf


    【解决方案1】:

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

    要将 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
    

    【讨论】:

    • 非常感谢!添加 converter.Options.MinPageLoadTime = 2 有帮助!
    • 如何将此 PDF 文件转换为字节数组而不是保存到物理文件中。请帮助我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 2021-07-25
    相关资源
    最近更新 更多