从 HTML 导出到 PDF (C#)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/589852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Export from HTML to PDF (C#)
提问by user29964
Possible Duplicate:
Convert HTML to PDF in .NET
可能的重复:
在 .NET 中将 HTML 转换为 PDF
In our applications we make html documents as reports and exports. But now our customer wants a button that saves that document on their pc. The problem is that the document includes images. You can create a word document with the following code:
在我们的应用程序中,我们将 html 文档制作为报告和导出。但是现在我们的客户想要一个按钮来将该文档保存在他们的 PC 上。问题是该文档包含图像。您可以使用以下代码创建一个 word 文档:
private void WriteWordDoc(string docName)
{
Response.Buffer = true;
Response.ContentType = "application/msword";
Response.AddHeader("content-disposition", String.Format("attachment;filename={0}.doc", docName.Replace(" ", "_")));
Response.Charset = "utf-8";
}
But the problem is that the images are just links an thus not embedded in the word document.
但问题是图像只是链接,因此没有嵌入到 Word 文档中。
Therefore I'm looking for an alternative PDF seems to be a good alternative, does anyone know a good pdf writer for C#? One that has some good references and has been tested properly?
因此,我正在寻找替代 PDF 似乎是一个不错的选择,有人知道 C# 的优秀 pdf 编写器吗?一个有一些很好的参考资料并且已经过正确测试?
采纳答案by Remo.D
I would opt for creating a PDF file on the server. There are many products that do so but you should research the one that works best in your case considering the following:
我会选择在服务器上创建一个 PDF 文件。有许多产品可以这样做,但您应该考虑以下因素研究最适合您的产品:
- Computer resources needed to create the PDF. If it's a complex document it may take too long and or slow down the response to other users.
- Number of concurrent users that will require this same function
- Cost (there are free solutions as well as heavy weight commercial products).
- 创建 PDF 所需的计算机资源。如果它是一个复杂的文档,它可能需要太长时间或减慢对其他用户的响应。
- 需要相同功能的并发用户数
- 成本(有免费的解决方案以及重量级的商业产品)。
I would not rely on Word format for that as PDF will give you some more guarantee that it will be readable in the future.
我不会依赖 Word 格式,因为 PDF 会给你更多的保证,它将来是可读的。
Also, the option of embedding hard links to the images don't seem a good idea to me. What if the user wants to open the document and the server is not accessible?
此外,将硬链接嵌入图像的选项对我来说似乎不是一个好主意。如果用户想打开文档而服务器无法访问怎么办?
回答by leppie
You would have to give the images absolute link to some avaible location on the internet.
您必须为图像提供指向 Internet 上某个可用位置的绝对链接。
Once the doc has been loaded into Word, saving the 'HTML' doc as a MSWord one, should include the images (or perhaps an option?).
将文档加载到 Word 后,将“HTML”文档另存为 MSWord 文档,应包括图像(或者可能是一个选项?)。
回答by Nikos Steiakakis
I've had the same problembut have not got around to solving it, since we settled to exporting an "image-less" document, since it did not have any images in the first place.
我遇到了同样的问题,但还没有解决它,因为我们决定导出一个“无图像”文档,因为它首先没有任何图像。
However, when searching for the problem I came across this article, about how to export a doc using XSLT. I haven't yet found the time to work on it, but maybe you can have a go at it
回答by Bryan
You have a bigger problem... saving the file generated is the prerogative of the browser. How the browser deals with any particular file stream, even when you set the content type, is entirely up to the browser. Your best bet is probably to use something like ABCpdf to convert the HTML/images into a PDF. I've had pretty good luck with their software, and they have decent support. Of course, this is a third party tool you'll have to install. Without doing that, your next best option is probably to create a zip of the HTML with images and other files (CSS, javascript?)... but that is going to take quite a bit of back-end logic.
你有一个更大的问题......保存生成的文件是浏览器的特权。浏览器如何处理任何特定的文件流,即使您设置了内容类型,也完全取决于浏览器。您最好的选择可能是使用 ABCpdf 之类的东西将 HTML/图像转换为 PDF。我在他们的软件上很幸运,他们有不错的支持。当然,这是您必须安装的第三方工具。如果不这样做,您的下一个最佳选择可能是使用图像和其他文件(CSS、javascript?)创建 HTML 的 zip 文件……但这将需要相当多的后端逻辑。
Some browsers have this feature built-in. You could ask your users to use that. :)
某些浏览器内置了此功能。您可以要求您的用户使用它。:)
回答by Cheeso
MS has a new page-description format "XPS" which can be generated simply from WPF the programming model, whether on the server or on a client. There's an XPS Reader app, like the PDF Reader, that allows users to view and print XPS docs. There's a simple API to produce an XPS doc.
MS 有一个新的页面描述格式“XPS”,它可以简单地从 WPF 编程模型生成,无论是在服务器上还是在客户端上。有一个 XPS 阅读器应用程序,如 PDF 阅读器,允许用户查看和打印 XPS 文档。有一个简单的 API 可以生成 XPS 文档。
回答by Constantine
Try PDF Duo .Netcomponent that converts HTML to PDF to/from string | file | url | stream. It is a small but very effictive library that you can use in your ASP.NET application.
试用PDF Duo .Net组件,将 HTML 转换为 PDF 到/从字符串 | 档案 | 网址 | 溪流。它是一个小但非常有效的库,您可以在 ASP.NET 应用程序中使用它。
回答by Joel Mueller
ExpertPDFdoes a decent job of converting HTML to PDF (including images). Internally, it uses a hosted copy of IE to render the HTML before converting it, which means this component won't work with Mono on Linux, and it means IE's quirks are your PDF's quirks. That said, it does a good job of rendering moderately complex layouts, and you can control pagination with CSS page-break-before and such.
ExpertPDF在将 HTML 转换为 PDF(包括图像)方面做得不错。在内部,它使用 IE 的托管副本在转换 HTML 之前呈现 HTML,这意味着该组件无法与 Linux 上的 Mono 一起使用,这意味着 IE 的怪癖就是您的 PDF 的怪癖。也就是说,它在呈现中等复杂的布局方面做得很好,并且您可以使用 CSS page-break-before 等来控制分页。
回答by scott
open source pdf stream generation .net assembly: http://sourceforge.net/projects/itextsharp/
开源pdf流生成.net程序集:http: //sourceforge.net/projects/itextsharp/
once you get the hang of it, you'll never use any 3rd party #%$^# or clog up your server doing IO's and taking up space for a temporary file ever again.
一旦掌握了它,您将永远不会使用任何 3rd 方#%$^# 或阻塞您的服务器执行 IO 并再次占用临时文件的空间。
回答by Julie
I like DocRaptor.comfor creating pdfs. It's a web app that works in any language, and it uses Prince XML, so the results are high quality.
我喜欢DocRaptor.com来创建 pdf。这是一个可以使用任何语言的网络应用程序,它使用 Prince XML,因此结果是高质量的。