2017-04-21 64 views
0

我正在使用它将html转换为pdf,但它什么都不做。没有错误或例外。我调试过,它有html内容,甚至在页面上显示,但不下载。我将HTML内容从数据库加载到隐藏字段中,并从后端分配给文字控件,并且我检查它是否显示值。为什么不能将我的页面转换为pdf?

代码:

private MemoryStream Export(string html="") 
    { 
     ltr.Text = hdnDescription.Value; 
     html = ltr.Text; 

     MemoryStream msOutput = new MemoryStream(); 
     TextReader reader = new StringReader(html); 

     // step 1: creation of a document-object 
     Document document = new Document(PageSize.A4, 30, 30, 30, 30); 

     // step 2: 
     // we create a writer that listens to the document 
     // and directs a XML-stream to a file 
     PdfWriter writer = PdfWriter.GetInstance(document, msOutput); 

     // step 3: we create a worker parse the document 
     HTMLWorker worker = new HTMLWorker(document); 

     // step 4: we open document and start the worker on the document 
     document.Open(); 
     worker.StartDocument(); 

     // step 5: parse the html into the document 
     worker.Parse(reader); 

     // step 6: close the document and the worker 
     worker.EndDocument(); 
     worker.Close(); 
     document.Close(); 

     return msOutput; 
    } 

的.aspx:

<%@ Page Title="Print Tender" EnableEventValidation="false" Language="C#" 
    MasterPageFile="~/Default.master" AutoEventWireup="true" CodeFile="printTender.aspx.cs" 
    Inherits="forms_general_printTender" %> 

<%--<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>--%> 
<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="Server"> 

     <asp:HiddenField ID="hdnDescription" runat="server" /> 
    <asp:Literal ID="ltr" runat="server"></asp:Literal> 

    <rsweb:ReportViewer ID="ReportViewer1" AsyncRendering="false" runat="server" Width="100%" Height="50%" Font-Names="Verdana" SizeToReportContent="false" Font-Size="8pt" WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt" /> 
</asp:Content> 
<asp:Content ID="Content3" ContentPlaceHolderID="ContentScript" runat="Server"> 
</asp:Content> 

回答

1

首先,HTMLWorker被XMLWorker,刚刚被取代的pdfHTML取代。 背后的许多版本不会帮助您的文档 - 工作流程。

其次,iText只转换静态HTML。 ASP页面不被视为静态内容。

相关问题