c# - 发送 pdf 声明而不保存在应用程序服务器上

标签 c# .net asp.net-mvc pdf-generation asp.net-4.0

要求:在公司模板上生成pdf格式的发票并通过电子邮件发送。

我使用的方法:

  1. 将公司模板放置在路径:~Content/InvoiceTemplate/
  2. 使用iTextsharp Pdf stamper,生成pdf,保存在路径:~/Content/reports/
  3. 在电子邮件模块中,选择上面生成的文件并附加到要发送的电子邮件

问题:生成的每张发票都存储在应用程序服务器上,使应用程序日益繁重。

问题:通过电子邮件发送生成的语音而不将其保存在应用程序服务器上的其他方法是什么?

代码:

    public static void WriteInTemplate(List<Models.Statement> statementList)
    {
        try
        {
            string invoiceNumber = statementList.FirstOrDefault().Invoice.ToString().Trim();

            using (Document document = new Document())
            {
                FileStream fileStream = new FileStream(HostingEnvironment.MapPath("~/Content/reports/" + invoiceNumber + ".pdf"), FileMode.Create);
                using (PdfSmartCopy smartCopy = new PdfSmartCopy(document, fileStream))
                {
                    document.Open();

                    int statementCounter = 0;
                    int numberOfItems = statementList.Count();
                    int remainingItems = numberOfItems;
                    int maxItemsPerPage = 17;
                    if (remainingItems > 0)
                    {
                        do
                        {
                            if (remainingItems < maxItemsPerPage)
                                maxItemsPerPage = remainingItems;


                            PdfReader pdfReader = new PdfReader(HostingEnvironment.MapPath("~/Content/InvoiceTemplate/invoiceTemplate.pdf"));
                            using (var memoryStream = new MemoryStream())
                            {
                                using (PdfStamper pdfStamper = new PdfStamper(pdfReader, memoryStream))
                                {
                                    string month = null;
                                    string day = null;
                                    string year = null;

                                    AcroFields pdfFields = pdfStamper.AcroFields;
                                    {//billing address
                                        pdfFields.SetField("BillToCompany", statementList.FirstOrDefault().BillToCompany.ToString().Trim().ToUpper());
                                        pdfFields.SetField("BillToContact", statementList.FirstOrDefault().BillToContact.ToString().Trim().ToUpper());
                                   }
           //---------------------snip------------------------------//
           //---------------------snip------------------------------//

                                    }
                                    {//invoice sum up
                                        double subTotal = Convert.ToDouble(statementList.FirstOrDefault().Subtotal);
                                        pdfFields.SetField("Subtotal", statementList.FirstOrDefault().Subtotal.ToString("0.00").Trim());

                                        double misc = Convert.ToDouble(statementList.FirstOrDefault().Misc);
                                        pdfFields.SetField("Misc", statementList.FirstOrDefault().Misc.ToString("0.00").Trim());

                                        double tax = Convert.ToDouble(statementList.FirstOrDefault().Tax);
                                        pdfFields.SetField("Tax", statementList.FirstOrDefault().Tax.ToString("0.00").Trim());

                                    }
                                    pdfStamper.FormFlattening = true; // generate a flat PDF 

                                }
                                pdfReader = new PdfReader(memoryStream.ToArray());
                                smartCopy.AddPage(smartCopy.GetImportedPage(pdfReader, 1));

                            }
                            remainingItems = remainingItems - maxItemsPerPage;

                        } while (remainingItems > 0);
                    }
                }
            }

            emailController.CreateMessageWithAttachment(invoiceNumber);
        }
        catch (Exception e)
        {
        }

    }

最佳答案

您可以尝试从内存流附加文件。您可以在 Google 中搜索“从内存流中附加 C# 文件”。

这是一个示例片段:

 mail.Attachments.Add(new Attachment(memoryStream, "example.txt", "text/plain"));

或者:

email attachment from the MemoryStream comes empty

http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/049420de-7e93-4fcb-9920-0c1cdf4ca420/

http://www.codeproject.com/KB/IP/InMemoryMailAttachment.aspx

关于c# - 发送 pdf 声明而不保存在应用程序服务器上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8930711/

相关文章:

javascript - 将 JavaScript 添加到 MVC 4 .NET Bootstrap Web 应用程序

c# - 如何使用 C# 将命令行输出重定向到 ASPX 页面?

c# - 生成的 WSDL 代理具有名为 "System"的类

.net - 引用两个相等的程序集,只有公钥不同

c# - 如何修复此错误 "Microsoft.Extensions.Azure: Could not load file or assembly ' System.Diagnostics.Tracing,版本=5.0.0.0,”

javascript - 如何在 MVC 5 _Layout 页面中加载外部 Javascript 文件

c# - 在 Razor View 中将 linq 与动态模型结合使用

c# - 验证 ABN(澳大利亚商业编号)

c# - 如何模拟自定义 ValueResolver 构造函数参数

.NET 核心 2 : How to check if the request is MIME multipart Content?