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

发布于 2024-12-03 14:09:49 字数 341 浏览 0 评论 0原文

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

你如何建议我去做这件事?我最好在视图上看到一个打印按钮,可以直接将其打印为 PDF。即没有弹出辅助窗口。但这可能不会破坏交易。

** 更新 ** 让我们暂时忘记表单元素。假设我的视图以与 PDF 中所需的格式相同的格式显示。如何将该视图打印为 PDF?

I'm working on a view in a ASP.NET MVC 2 application. This view will contain fixed text but it will also contain text boxes, checkboxes, and perhaps a Telerik Grid that can be updated from a user. ** This form isn't in a fixed format since there could be a 1...N number of items listed. ** We want to be able to print this view to a PDF. The PDF we want to print off will just look like the view but preferrably it will have just the text from the text boxes and not the text box border. The same goes for the Telerik grid.

How to you recommend I go about doing this? Preferrably I like to see a print button on the view that will directly print it to a PDF. i.e. No secondary window that pops up. That may not be a deal breaker though.

** Update **
Let's forget about the form elements for a second. Let's say my view is displayed in the same format that I want in my PDF. How to print that view into a PDF?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幸福丶如此 2024-12-10 14:09:49

最简单的方法是创建一个单独的打印操作,该操作返回使用 iTextSharp< 等库动态生成的 PDF 的 FileResult /a>

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

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

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

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

The simplest way to do this is to create a separate Print action that returns a FileResult of a PDF generated on the fly with a library like iTextSharp

You would not be able to completely reuse the HTML form as is in the PDF document, since you don't want to use text boxes, but your could generate an HTML view that matches your desired PDF and then use iTextSharp to save that HTML as a PDF.

Alternatively, you could use the iTextSharp library to build up a PDF from scratch and have much more control, but this might be a bit more difficult.

From your controller the simplest way to return the PDF without a secondary window is to have your action method return:

return File(iTextSharpByteArray, "application/pdf", "nameOfFileUserWillDownload.pdf");
我的痛♀有谁懂 2024-12-10 14:09:49

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

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

Most free open source PDF .dlls are difficult to programatically create HTML in a PDF (mostly due to limited support for HTML tags etc.).

A paid for one is much more simple eg. http://www.html-to-pdf.net/ With this you can just point the converter to a template page and it will work. Even javascript and Flash content etc will also be parsed and included (statically) in the final PDF.

爱要勇敢去追 2024-12-10 14:09:49

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

        }

You can make an rdlc report as desired and call at the click of a print button/link in your view, through a controller function.

in your view

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

in you controller

    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);

        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文