【问题标题】:ASP.NET MVC Converting HTML page containing Form to PDFASP.NET MVC 将包含表单的 HTML 页面转换为 PDF
【发布时间】:2011-09-08 13:58:48
【问题描述】:

我正在处理 ASP.NET MVC 2 应用程序中的视图。此视图将包含固定文本,但也将包含文本框、复选框,也许还有一个 Telerik Grid,可以从用户那里更新。 ** 此表单不是固定格式,因为可能列出了 1...N 个项目。 ** 我们希望能够将此视图打印为 PDF。我们要打印的 PDF 看起来就像视图,但最好只有文本框中的文本,而不是文本框边框。 Telerik 网格也是如此。

您如何建议我这样做?最好我喜欢在视图上看到一个打印按钮,它将直接打印到 PDF 中。即没有弹出辅助窗口。不过,这可能不会破坏交易。

** 更新 ** 让我们暂时忘记表单元素。假设我的视图以我想要的 PDF 格式显示。如何将该视图打印成 PDF?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-2 pdf


    【解决方案1】:

    执行此操作的最简单方法是创建一个单独的 Print 操作,该操作返回使用 iTextSharp 之类的库动态生成的 PDF 的 FileResult

    您将无法完全重用 PDF 文档中的 HTML 表单,因为您不想使用文本框,但您可以生成与所需 PDF 匹配的 HTML 视图,然后使用 iTextSharp 保存该 HTML 为 PDF。

    或者,您可以使用 iTextSharp 库从头开始构建 PDF 并拥有更多控制权,但这可能会有点困难。

    从您的控制器返回 PDF 而不使用辅助窗口的最简单方法是让您的操作方法返回:

    return File(iTextSharpByteArray, "application/pdf", "nameOfFileUserWillDownload.pdf");
    

    【讨论】:

    • 我已经测试了 ITextSharp 以从头开始为应用程序的不同部分创建 PDF,但它很混乱,而且似乎无法按照我想要的方式进行格式化。由于我们熟悉水晶报表,因此我们可能会从头开始生成一些东西。至于这个问题,您建议我创建一个没有输入控件(即文本框、网格)的新输出视图是正确的,它带有一个可以生成 PDF 的打印按钮。是否必须显示该输出视图,或者我可以使用您提到的方法直接转到 PDF 输出。
    • 是的,您可以替换任何 PDF 生成库来代替 iTextSharp。至于正在显示的视图,只要您返回一个文件,就像我在示例代码中所做的那样,用户将获得一个“保存/opn”对话框,而不是一个 HTML 页面。
    【解决方案2】:

    大多数免费的开源 PDF .dll 很难以编程方式在 PDF 中创建 HTML(主要是由于对 HTML 标记等的支持有限)。

    付费购买要简单得多,例如。 http://www.html-to-pdf.net/ 有了这个,您只需将转换器指向模板页面,它就可以工作。甚至 javascript 和 Flash 内容等也将被解析并(静态)包含在最终的 PDF 中。

    【讨论】:

      【解决方案3】:

      您可以根据需要制作 rdlc 报告,并通过控制器功能单击视图中的打印按钮/链接进行调用。

      在你看来

          Html.ActionLink("Print", "Print", new { id = c.sid }) 
      

      在你的控制器中

          public ActionResult Print(int id)
                  {
                      string unitc = Session["unit"].ToString();
      
                      ctid= unitc;//class level variable used in detailreport function  
                      brid = id;//class level variable used in detailreport function
                      return DetailsReport();
      
                  }
      
      
      
          FileContentResult DetailsReport()
              {
      
                  LocalReport localReport = new LocalReport();
      
                  localReport.ReportPath = Server.MapPath("~/Reports/rptinvoice.rdlc");
      
                  InvoiceRepository ivr = new InvoiceRepository();
      
                  if (localReport.DataSources.Count > 0)
                  {
                      localReport.DataSources.RemoveAt(0);
                      localReport.DataSources.RemoveAt(1);
                      localReport.DataSources.RemoveAt(2);
      
                  }
                  localReport.Refresh();
      
                  ReportDataSource reportDataSource = new ReportDataSource("DataSet1", ivr.GetSales(ctid));
      
                  localReport.SetParameters(new ReportParameter[] { new ReportParameter("ct_id", ctid.ToString()), new ReportParameter("ct_br_id", brid.ToString()) });
      
      
                  localReport.DataSources.Add(reportDataSource);
      
      
                  string reportType = "PDF";
      
                  string mimeType;
      
                  string encoding;
      
                  string fileNameExtension;
      
      
      
      
                  string deviceInfo =
      
                  "<DeviceInfo>" +
      
                  "  <OutputFormat>PDF</OutputFormat>" +
      
                  "  <PageWidth>8.5in</PageWidth>" +
      
                  "  <PageHeight>11in</PageHeight>" +
      
                  "  <MarginTop>0.2in</MarginTop>" +
      
                  "  <MarginLeft>0.05in</MarginLeft>" +
      
                  "  <MarginRight>0.05in</MarginRight>" +
      
                  "  <MarginBottom>0.1in</MarginBottom>" +
      
                  "</DeviceInfo>";
      
      
      
                  Warning[] warnings;
      
                  string[] streams;
      
                  byte[] renderedBytes;
      
                  localReport.EnableExternalImages = true;
      
                  //Render the report
      
                  try
                  {
      
                  renderedBytes = localReport.Render(
      
                  reportType,
      
                  deviceInfo,
      
                  out mimeType,
      
                  out encoding,
      
                  out fileNameExtension,
      
                  out streams,
      
                  out warnings);
      
      
      
                  }
                  catch (Exception Ex)
                  {
                      ViewData["ResultP"] = Ex.Message + ",<br>" + Ex.InnerException.Message;
                      throw;
                  }
      
      
                  return File(renderedBytes, mimeType);
      
              }
      

      【讨论】:

        猜你喜欢
        • 2011-07-01
        • 2011-01-13
        • 1970-01-01
        • 1970-01-01
        • 2012-05-11
        • 2021-05-13
        • 2019-08-04
        • 2014-08-21
        相关资源
        最近更新 更多