【问题标题】:PDF generation in C#在 C# 中生成 PDF
【发布时间】:2010-09-26 19:00:37
【问题描述】:

我们目前以纯文本或 html 格式发送电子邮件通知。我们的环境是 C#/.NET/SQL Server。

我想知道是否有人推荐特定的解决方案。我看到了两种方法:

  • 使用第三方库将当前电子邮件动态转换为 pdf 并将 pdf 作为附件发送

  • 使用 SSRS 允许用户导出 pdf 报告(最终可能会有 SSRS 推送报告)

我对第三方库持开放态度(尤其是如果它们是开源且免费的)。 SSRS 似乎是最简单、最容易的方法。有人有什么建议吗?

【问题讨论】:

  • 所有电子邮件都应该是纯文本。 HTML 很糟糕,而 PDF 更糟糕。你能向 SO为什么解释一下你用这种方式折磨你的人吗?
  • 添加无法在 PDF 文本电子邮件中轻松表示的附加信息可能很有用,但将现有电子邮件转换为 PDF 没有意义。

标签: c# .net sql-server pdf reporting-services


【解决方案1】:

您可以使用iTextSharp 将您的html 页面转换为pdf。这是一个例子:

class Program
{
    static void Main(string[] args)
    {
        string html = 
@"<html>
<head>
  <meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"" />
</head>
<body>
  <p style=""color: red;"">Hello World</p>
</body>
</html>";

        Document document = new Document(PageSize.A4);
        using (Stream output = new FileStream("out.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
        using (StringReader htmlReader = new StringReader(html))
        using (XmlTextReader reader = new XmlTextReader(htmlReader))
        {
            PdfWriter.GetInstance(document, output);
            HtmlParser.Parse(document, reader);
        }

    }
}

【讨论】:

  • 基本字符串只是一个 div 部分
    stuff
    。该文件已创建,但无法打开。您发布的代码是有效的还是只是模型?
【解决方案2】:

iText-Sharp
工作方式与 java 版本非常相似,并且有很好的文档和多本书可供使用。

【讨论】:

    【解决方案3】:

    如果只需要简单的html->pdf转换,看wkhtmltopdf。它是基于 webkit 的免费、开源、命令行实用程序。

    如果您想要更复杂的东西,例如准备多个模板或存储 pdf 输出的历史记录。看报表服务器jsreport

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-04
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-16
      • 2021-09-26
      相关资源
      最近更新 更多