我正在使用SelectPdf處理將網頁轉換爲pdf文件的任務。 SelectPdf不支持動態頁面。所以我想用Ajax將網頁作爲html傳遞。Ajax無法將HTML頁面作爲參數傳遞給後面的代碼
由於某些原因,當我通過普通的字符串它的作品,但當我改變使用變量(與HTML作爲價值)它沒有。我不知道html內容是否太大,但是我嘗試了更少的內容,仍然是同樣的問題。任何幫助將不勝感激。
該項目語言爲VB.Net,頁面爲vbhtml,後面的代碼爲控制器。
請參閱我已經實現的代碼如下:
VIEW
var btn = $('#BtnCreateHtmlToPdf');
btn.click(function() {
var theHtml = document.documentElement.innerHTML;
//Just to see there is a value
alert(theHtml)
$(function() {
$.ajax({
type: 'post',
url: "/CreatHtmlToPdf/CreatePdf",
dataType: "html",
data: { HTML: theHtml }
})
.done(function (results) {
alert("Html data: " + results);
});
});
});
後面的代碼
Public Class CreatHtmlToPdfController
Inherits Controller
' GET: CreatHtmlToPdf
Function Index() As ActionResult
Return View()
End Function
<HttpPost()>
Function CreatePdf(ByVal HTML As String) As ActionResult
Dim doc As PdfDocument
' read parameters from the webpage
Dim htmlString As String = HTML
' instantiate a html to pdf converter object
Dim converter As New HtmlToPdf()
' create a new pdf document converting an url
If (HTML <> String.Empty) Then
doc = converter.ConvertHtmlString(htmlString)
End If
' save pdf document
Dim pdf As Byte() = doc.Save()
' close pdf document
doc.Close()
' return resulted pdf document
Dim fileResult As FileResult = New FileContentResult(pdf, "application/pdf")
fileResult.FileDownloadName = "Results_page.pdf"
Return fileResult
End Function
'Declaration
'Public Property EnablePageMethods As Boolean
End Class
你有沒有嘗試過dataType到json? – casper123