2011-09-08 122 views
2

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

你如何推荐我去做这件事?最好我喜欢在视图上看到一个打印按钮,它将直接打印到PDF上。即没有弹出的辅助窗口。尽管如此,这可能不是一个问题。

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

回答

3

要做到这一点,最简单的方法是创建一个返回与库中的苍蝇一样iTextSharp

生成的PDF的FileResult一个单独的打印操作,您将无法完全重复使用的HTML形式是在PDF文档中,由于您不想使用文本框,但是您可以生成与所需PDF匹配的HTML视图,然后使用iTextSharp将该HTML保存为PDF。

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

从你的控制器返回PDF无辅助窗口最简单的方法是让你的操作方法的返回:

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

我已经测试了iTextSharp的从头创建一个PDF的应用程序的不同部分,但它是凌乱的,它没有似乎可以按照我想要的方式进行格式化。因为我们熟悉它,所以我们很可能会从CrystalReport的路线生成一些东西。至于这个问题,你建议我创建一个没有输入控件(即文本框,网格)的新输出视图,并使用打印按钮来生成PDF。该输出视图是否必须显示,或者我可以使用您提到的方法直接转到PDF输出。 – Elim99

+0

是的,您可以替换任何PDF生成库来代替iTextSharp。 就显示的视图而言,只要你返回一个文件,就像我在示例代码中那样,用户将得到一个“save/opn”对话框而不是HTML页面。 – nikmd23

0

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

为一个人付费更简单,例如。 http://www.html-to-pdf.net/有了这个,您可以将转换器指向模板页面,它将起作用。即使JavaScript和Flash内容等也将被解析并包含在静态的最终PDF中。

0

您可以根据需要制作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); 

     }