gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:09:39 25 4
gpt4 key购买 nike

要求:在公司模板上生成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# Attach file from memory stream”。

这是一个示例片段:

 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com