ASP.NET MVC 将包含表单的 HTML 页面转换为 PDF
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的方法是创建一个单独的打印操作,该操作返回使用 iTextSharp< 等库动态生成的 PDF 的 FileResult /a>
您将无法完全重用 PDF 文档中的 HTML 表单,因为您不想使用文本框,但您可以生成与所需 PDF 匹配的 HTML 视图,然后使用 iTextSharp 保存将 HTML 转换为 PDF。
或者,您可以使用 iTextSharp 库从头开始构建 PDF 并拥有更多控制权,但这可能会更困难一些。
从控制器中返回 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:
大多数免费开源 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.
您可以根据需要制作 rdlc 报告,并通过控制器功能单击视图中的打印按钮/链接进行调用。
在你的
控制器中
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
in you controller