Esempio n. 1
0
        private static void GenerateReportPdf(Report report, string outputDirectory)
        {
            var path = $"{outputDirectory}\\{report.Id} Receipts.pdf";

            try
            {
                using (var outputStream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
                {
                    using (var doc = new Document())
                    {
                        using (var merge = new PdfSmartCopy(doc, outputStream))
                        {
                            doc.Open();
                            foreach (var entry in report.Entries)
                            {
                                try
                                {
                                    if (!string.IsNullOrEmpty(entry.Path))                                     //If we have already set a path for the entry, just pull that file
                                    {
                                        var bytes = File.ReadAllBytes(entry.Path);
                                        if (bytes.Length == 0)
                                        {
                                            throw new Exception($"File Was Empty: {entry.Path}");
                                        }
                                        merge.AddDocument(new PdfReader(bytes));
                                    }
                                    else if (entry.Type == ReportEntryType.Concur)                                    //If we don't have a path and we are a concur receipt, process that image
                                    {
                                        if (entry.Image == null)
                                        {
                                            throw new Exception("No Image Found for this Entry.");
                                        }

                                        var receipt = entry.Image;
                                        ConcurClient.DownloadImage(receipt);
                                        switch (receipt.ContentType)
                                        {
                                        case "pdf":
                                            merge.AddDocument(new PdfReader(receipt.Data));
                                            break;

                                        case "image":
                                        default:
                                            using (var output = new MemoryStream())
                                            {
                                                using (var document = new Document())
                                                {
                                                    PdfWriter.GetInstance(document, output);
                                                    document.Open();

                                                    var image = Image.GetInstance(receipt.Data);
                                                    image.ScaleToFit(document.PageSize);
                                                    document.Add(image);
                                                    document.NewPage();
                                                }
                                                var bytes = output.ToArray();
                                                merge.AddDocument(new PdfReader(bytes));
                                            }
                                            break;
                                        }
                                    }
                                    else                                     //If we don't have a path, and we aren't a concur document, wtf...
                                    {
                                        throw new Exception($"No File for: {entry.Path}");
                                    }
                                }
                                catch (Exception e)
                                {
                                    report.HasError = true;
                                    ErrorLogger.LogError(entry.Key, e.Message);
                                }
                            }

                            if (merge.CurrentPageNumber <= 1)
                            {
                                merge.PageEmpty = true;
                                merge.NewPage();
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                report.HasError = true;
                ErrorLogger.LogError(report.Id, e.Message);
            }

            if (report.HasError)
            {
                //Lets get rid of our problem child
                File.Delete(path);
            }
        }