2

我在没有 runat="server" 的 .aspx pahe 中有名为 RPTHtml 的字段

 <asp:HiddenField ID="RPTHtml"  />

并在javascript中添加此代码并调用此页面的pagemethod

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实现。提前致谢!!!

4

1 回答 1

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方法代码。希望这会有所帮助。

于 2013-05-25T06:46:06.707 回答