Asp.net mvc ASP.NET MVC将包含表单的HTML页面转换为PDF

Asp.net mvc ASP.NET MVC将包含表单的HTML页面转换为PDF,asp.net-mvc,asp.net-mvc-2,pdf,Asp.net Mvc,Asp.net Mvc 2,Pdf,我正在处理ASP.NET MVC 2应用程序中的一个视图。此视图将包含固定文本,但也将包含文本框、复选框,可能还包含用户可以更新的Telerik网格。**此表单不是固定格式,因为可能列出了1…N个项目。**我们希望能够将此视图打印为PDF。我们想要打印出来的PDF看起来就像视图,但最好是文本框中的文本,而不是文本框边框。Telerik网格也是如此 你建议我怎么做?我更喜欢在视图上看到一个打印按钮,它可以直接打印成PDF格式。i、 e.没有弹出的辅助窗口。不过,这可能不会破坏交易 **更新** 让

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

你建议我怎么做?我更喜欢在视图上看到一个打印按钮,它可以直接打印成PDF格式。i、 e.没有弹出的辅助窗口。不过,这可能不会破坏交易

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

最简单的方法是创建一个单独的打印操作,该操作返回一个动态生成的PDF文件结果,其中包含一个库,如

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

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

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

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

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


一个付费的更简单,例如,你可以把转换器指向一个模板页面,它就可以工作了。甚至javascript和Flash内容等也将被解析并(静态地)包含在最终PDF中。

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

        }
public ActionResult打印(int-id)
{
字符串unitc=Session[“unit”].ToString();
ctid=unitc;//在detailreport函数中使用的类级别变量
brid=id;//在detailreport函数中使用的类级别变量
返回DetailsReport();
}
FileContentResult详细信息报告()
{
LocalReport LocalReport=新建LocalReport();
localReport.ReportPath=Server.MapPath(“~/Reports/rptinvoice.rdlc”);
InvoiceRepository ivr=新的InvoiceRepository();
如果(localReport.DataSources.Count>0)
{
localReport.DataSources.RemoveAt(0);
localReport.DataSources.RemoveAt(1);
localReport.DataSources.RemoveAt(2);
}
localReport.Refresh();
ReportDataSource ReportDataSource=新的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”;
字符串模拟类型;
字符串编码;
字符串文件名扩展名;
字符串设备信息=
"" +
“PDF”+
“8.5英寸”+
“11英寸”+
“0.2英寸”+
“0.05英寸”+
“0.05英寸”+
“0.1英寸”+
"";
警告[]警告;
字符串[]流;
字节[]渲染字节;
localReport.EnableExternalImages=true;
//提交报告
尝试
{
renderedBytes=localReport.Render(
报告类型,
deviceInfo,
输出mimeType,
输出编码,
输出文件名扩展名,
流出的溪流,
发出警告);
}
捕获(例外情况除外)
{
ViewData[“ResultP”]=Ex.Message+,
“+Ex.InnerException.Message; 投掷; } 返回文件(renderdbytes,mimeType); }
我已经测试过ITextSharp,可以从头开始为应用程序的不同部分创建PDF,但它很混乱,似乎无法按照我想要的方式格式化。我们可能会选择Crystal Report路线,从零开始生成一些东西,因为我们熟悉它。对于这个问题,您建议我创建一个没有输入控件(即文本框、网格)的新输出视图,并使用打印按钮生成PDF是正确的。是否必须显示输出视图,或者我是否可以使用您提到的方法直接转到PDF输出。是的,您可以替换任何PDF生成库来代替iTextSharp。就显示的视图而言,只要您返回一个文件,就像我在示例代码中看到的那样,用户将得到一个“save/opn”对话框,而不是一个HTML页面。