【发布时间】: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“)”?谢谢
【问题讨论】: