vb.net selectPDF 不会从 HTML 字符串中保存 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36334960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
selectPDF doesnt save PDF from HTML string
提问by kaplievabell
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.
我正在尝试使用 SelectPDF for VB.NET 将页面中的 html 保存为 pdf。我从带有 html 的网页 json 传递来保存,在服务器端获取它。看起来转换器成功转换了 html(没有抛出错误),但它在保存部分中断。
Javascript:
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
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:
我在那个位置保存了空的 PDF。我也尝试用 html 保存字符串,但没有成功,例如:
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
是否可以使用此 SelectPDF 库从代表 html 的字符串中保存 PDF?如果是的话,为什么我会收到错误“doc.Save(Response, False,“C:\MyProject\Pdf\Sample.pdf”)”的任何指针?谢谢
采纳答案by WebDev3r
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).
如果您只需要将 pdf 文档保存在磁盘上,则不应使用方法调用 doc.Save(Response, False, "C:\MyProject\Pdf\Sample.pdf")。doc.Save(Response, False, "Sample.pdf") 的目的是将 PDF 发送到浏览器并建议下载名称(Sample.pdf - 无路径)。
To save the PDF on disk, simply use doc.Save("C:\MyProject\Pdf\Sample.pdf").
要将 PDF 保存在磁盘上,只需使用 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.
在您确定使用简单的 html 转换运行正常后(应该不是问题),请检查您发送到转换方法 ConvertHtmlString 的 html 和 baseUrl。将它们记录到一个文件中。看看它们是否是您所期望的。
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
由于您使用的是 javascript,加载可能需要一些时间,因此请尝试在转换前输入延迟: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

