我在.aspx pahe中有字段nammed RPTHtml而没有runat =“server”
<asp:HiddenField ID="RPTHtml" />
在javascript中添加此代码并调用此页面的页面方法
document.getElementById('RPTHtml').setValue =window.encodeURI
(document.getElementById(eval(result)[0].columnLineConfig.divToRender).innerHTML);
PageMethods.getHTMLToPdf(eval(result)[0].columnLineConfig.divToRender, srcurl);
现在,在我的页面方法
中<WebMethod()> _
<System.Web.Script.Services.ScriptMethod()> _
Public Shared Function getHTMLToPdf(ByVal div As String, ByVal InnerHTMl As String) As Boolean
Dim pdfDocument As PdfDocument.Document = Nothing
Dim pdfConverter As PdfConverter = New PdfConverter()
Dim pdfBytes As Byte() = Nothing
Dim strHtml As String = ""
Dim htmlreport As String = ""
Dim ReportFileName As String = ""
Dim Response_1 As HttpResponse = System.Web.HttpContext.Current.Response
If InnerHTMl.ToString() = "" Then
Exit Function
End If
htmlreport = InnerHTMl.ToString()
htmlreport = HttpUtility.UrlDecode(htmlreport)
Response_1.Clear()
Response_1.Buffer = True
Response_1.ClearContent()
Response_1.ClearHeaders()
ReportFileName = GetReportName() + ".pdf"
pdfConverter = New Winnovative.WnvHtmlConvert.PdfConverter()
pdfConverter.AvoidImageBreak = True
pdfConverter.AvoidTextBreak = True
pdfConverter.TruncateOutOfBoundsText = True
pdfConverter.LicenseKey = "LicenseKey"
pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4
pdfConverter.PdfDocumentOptions.StretchToFit = True
pdfBytes = pdfConverter.GetPdfBytesFromHtmlString(htmlreport)
Response_1.AddHeader("Content-type", "application/pdf")
Response_1.AddHeader("Content-Disposition", "attachment; filename=" + ReportFileName)
Response_1.BinaryWrite(pdfBytes)
Response_1.Flush()
Response_1.End()
End Function
它没有提供任何输出,只是成功运行,但没有发生下载pdf的提示。
我错过了什么?或者它无法通过pagemethod实现。 提前谢谢!!!
答案 0 :(得分:0)
在您的asp.net页面中提供一个链接,如
<a href="javascript:ExportReport();">Export To Excel</a>
在javascript中(我使用jquery做同样的事情),动态创建HTML表单并提交它,Export.aspx
是生成pdf的页面
function ExportReport() {
var frmT = $([
'<form id="frmExport" method="post" action="Export.aspx" target="_blank" style="display:none;">',
'<input type="hidden" name="pdfContents" id="tableContents" />',
'</form>'
].join('')).appendTo('body')[0]
var hdnTbl = $("#pdfContents", frmT);
hdnTbl.val( divRep.html() ); // divRep.html() is the inner html you want to convert to PDF
frmT.submit();
}
由于您要在隐藏字段中提交HTML,因此您需要在ValidateRequest
页面中启用Export.aspx
属性
ValidateRequest="false"
另外,你需要在web.config中启用maxrequestlength,如下所示,
<httpRuntime requestValidationMode="2.0" maxRequestLength="1024" /> <!-- 1024 kb -->
在</system.web>
结束标记之前。如果您使用的是.net 2.0,则可以省略requestValidationMode="2.0"
属性。您可以在export.aspx页面加载方法中使用相同的getHTMLToPdf
方法代码。
希望这会有所帮助。