【问题标题】:Why doesn't itext convert my page to pdf?为什么 itext 不将我的页面转换为 pdf?
【发布时间】:2017-04-21 09:23:22
【问题描述】:

我正在使用它来将 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>

【问题讨论】:

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


    【解决方案1】:

    首先,HTMLWorker 被 XMLWorker 取代,而 XMLWorker 刚刚被 pdfHTML 取代。 支持这么多版本对您的文档 - 工作流程没有帮助。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-01
      • 2011-01-13
      • 1970-01-01
      • 1970-01-01
      • 2011-10-12
      • 2020-09-04
      相关资源
      最近更新 更多