2017-04-21 98 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頁面不被視爲靜態內容。

相關問題