为什么没有将我的页面转换为pdf?

时间:2017-04-21 09:23:22

标签: c# asp.net .net c#-4.0 itext

我使用它将html转换为pdf,但它什么也没做。没有错误或异常。我调试了它有html内容甚至在页面上显示但没有下载。我正在从数据库中将HTML内容加载到隐藏字段中,并从后端分配到文字控件,我检查它显示了值。

代码:

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 个答案:

答案 0 :(得分:1)

首先,HTMLWorker被XMLWorker取代,后者刚被pdfHTML取代。 支持这么多版本将无助于您的文档 - 工作流程。

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