Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 发送pdf语句而不在应用程序服务器上保存_C#_.net_Asp.net Mvc_Pdf Generation_Asp.net 4.0 - Fatal编程技术网

C# 发送pdf语句而不在应用程序服务器上保存

C# 发送pdf语句而不在应用程序服务器上保存,c#,.net,asp.net-mvc,pdf-generation,asp.net-4.0,C#,.net,Asp.net Mvc,Pdf Generation,Asp.net 4.0,要求:在公司模板上生成pdf格式的发票并通过电子邮件发送 我使用的方法: 将公司模板放置在路径:~Content/InvoiceTemplate/ 使用iTextsharp Pdf母版,生成Pdf,并将其保存在路径:~/Content/reports处/ 在电子邮件模块中,选择上面生成并附加到要发送的电子邮件的文件 问题:生成的每个发票都存储在应用程序服务器上,使应用程序一天比一天沉重 问题:在电子邮件中发送生成的语音而不将其保存在应用服务器上的另一种方法是什么 代码: public s

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

我使用的方法

  • 将公司模板放置在路径:~Content/InvoiceTemplate/
  • 使用iTextsharp Pdf母版,生成Pdf,并将其保存在路径:~/Content/reports处/
  • 在电子邮件模块中,选择上面生成并附加到要发送的电子邮件的文件
  • 问题:生成的每个发票都存储在应用程序服务器上,使应用程序一天比一天沉重

    问题:在电子邮件中发送生成的语音而不将其保存在应用服务器上的另一种方法是什么

    代码:

        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)
            {
            }
    
        }
    
    公共静态void WriteInTemplate(列表语句列表)
    {
    尝试
    {
    字符串invoiceNumber=statementList.FirstOrDefault().Invoice.ToString().Trim();
    使用(文档=新文档())
    {
    FileStream FileStream=newfilestream(HostingEnvironment.MapPath(“~/Content/reports/”+invoiceNumber+“.pdf”)、FileMode.Create);
    使用(PdfSmartCopy smartCopy=新的PdfSmartCopy(文档、文件流))
    {
    document.Open();
    int语句计数器=0;
    int numberOfItems=statementList.Count();
    int remainingItems=numberOfItems;
    int maxItemsPerPage=17;
    如果(剩余项>0)
    {
    做
    {
    如果(剩余项0);
    }
    }
    }
    emailController.CreateMessageWithAttachment(发票号码);
    }
    捕获(例外e)
    {
    }
    }
    
    您可以尝试从内存流附加文件。你可以在谷歌搜索“C#从内存流附加文件”

    以下是一个示例片段:

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


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

    以下是一个示例片段:

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


    如果pdf文件不是太大,并且您没有使用服务器场,并且您没有数百万人同时生成发票


    然后,您可以始终使用MemoryStream并将内存流传递给您的电子邮件服务。

    如果pdf文件不是太大,并且您没有使用服务器场,并且您没有数百万人同时生成发票


    然后,您可以始终使用MemoryStream并将内存流传递给电子邮件服务。

    您应该尝试在临时文件夹中创建文件,而不是在应用程序目录中创建文件。。当你处理完文件后,你应该删除它们。。这样,文件就不会占用驱动器上太多空间

    这是一个tempfile类,我与iTextSharp一起使用它在填写表单后导出pdf

      sealed class TempFile : IDisposable
        {
            string path;
            public TempFile() : this(System.IO.Path.GetTempFileName()) { }
    
            public TempFile(string path)
            {
                if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path");
                this.path = path;
            }
            public string Path
            {
                get
                {
                    if (path == null) throw new ObjectDisposedException(GetType().Name);
                    return path;
                }
            }
            ~TempFile() { Dispose(false); }
            public void Dispose() { Dispose(true); }
            private void Dispose(bool disposing)
            {
                if (disposing)
                {
                    GC.SuppressFinalize(this);
                }
                if (path != null)
                {
                    try { File.Delete(path); }
                    catch { } // best effort
                    path = null;
                }
            }
        }
    
    你应该试试

    using(TempFile file = new TempFile())
    {
    .....= new FileStream(file.Path,.....)
    //pdf form filling using iTextSharp
    
    var arry = GetBytesArray(file.Path);
    //Send Array to response and set content type to pdf..
    
    }
    

    您应该尝试在临时文件夹中创建文件,而不是在应用程序目录中创建文件。。当你处理完文件后,你应该删除它们。。这样,文件就不会占用驱动器上太多空间

    这是一个tempfile类,我已将它与iTextSharp一起用于e