Ejemplo n.º 1
0
        /// <summary>
        /// Load pdf page except pdf source.it only render the page bound.
        /// </summary>
        /// <param name="pdfDoc"></param>
        void LoadPagesWithoutSource(PdfDocument pdfDoc)
        {
            double pageOffset = 0;

            for (uint i = 0; i < pdfDoc.PageCount; i++)
            {
                using (var page = pdfDoc.GetPage(i))
                {
                    var zoomRate = this.ActualWidth / page.Size.Width;

                    var pdfProvider = new PdfProvider
                    {
                        Bound = new Bound
                        {
                            Width          = this.ActualWidth,
                            Height         = page.Size.Height * zoomRate,
                            VerticalOffset = pageOffset
                        },
                        Index = (int)i
                    };

                    PdfPages.Add(pdfProvider);
                    pageOffset += pdfProvider.Bound.Height;
                }
            }
        }
        public static void CreateFamilyVisitsReport(Family family, ICollection <Visit> visits)
        {
            FileStream fs = new FileStream(GetDestinationFolder("FamilyVisitsReport.pdf"), FileMode.Create);

            PdfHeaderFooter headerFooter = new PdfHeaderFooter();

            var doc = PdfProvider.GetPage(fs, headerFooter);

            StringBuilder familyMembers = new StringBuilder();

            foreach (var member in family.FamilyMembers)
            {
                familyMembers.AppendLine($"{member.FirstName} {member.LastName}");
            }

            StringBuilder familyChildren = new StringBuilder();

            foreach (var child in family.Children)
            {
                familyChildren.AppendLine($"{child.FirstName} {child.LastName}");
            }

            string[] userTableCells =
            {
                "Family Id",    "Family Name",    "Family Members",         "Family children",         "Assigned Staff Member",
                $"{family.Id}", $"{family.Name}", familyMembers.ToString(), familyChildren.ToString(),
                $"{family.AssignedStaffMember.FirstName} {family.AssignedStaffMember.LastName}"
            };

            var userTable = PdfProvider.CreateTable(5, userTableCells, 20);

            string[] userVisitsCells = { "Date", "Description", "Visit type" };

            var userVisits = PdfProvider.CreateTable(3, userVisitsCells, 50, "User visits table");

            foreach (Visit visit in visits)
            {
                string[] information = { visit.Date.ToShortDateString(), visit.Description, visit.VisitType.Name };
                PdfProvider.FillTable(userVisits, information);

                // userVisits.AddCell(visit.Date.ToShortDateString());
                // userVisits.AddCell(visit.Description);
                // userVisits.AddCell(visit.VisitType.Name);
                // userVisits.AddCell(familyName.Name);
            }

            doc.Open();

            doc.Add(userTable);

            doc.Add(userVisits);

            doc.Close();

            fs.Close();
        }
        public static void CreateUserReport(User user, ICollection <Visit> visits, IDataFactory dataFactory)
        {
            FileStream fs = new FileStream(GetDestinationFolder("UserReport.pdf"), FileMode.Create);

            PdfHeaderFooter headerFooter = new PdfHeaderFooter();

            var doc = PdfProvider.GetPage(fs, headerFooter);

            string[] userTableCells =
            {
                "First Name",        "Last Name",        "Username",
                $"{user.FirstName}", $"{user.LastName}", $"{user.UserName}"
            };

            var userTable = PdfProvider.CreateTable(3, userTableCells, 20);

            string[] userVisitsCells = { "Date", "Description", "Visit type", "Family name" };

            var userVisits = PdfProvider.CreateTable(4, userVisitsCells, 50, "User visits table");

            foreach (Visit visit in visits)
            {
                var      familyName  = dataFactory.FindFamily(visit.FamilyId);
                string[] information = { visit.Date.ToShortDateString(), visit.Description, visit.VisitType.Name, familyName.Name };
                PdfProvider.FillTable(userVisits, information);

                // userVisits.AddCell(visit.Date.ToShortDateString());
                // userVisits.AddCell(visit.Description);
                // userVisits.AddCell(visit.VisitType.Name);
                // userVisits.AddCell(familyName.Name);
            }

            doc.Open();

            doc.Add(userTable);

            doc.Add(userVisits);

            doc.Close();

            fs.Close();
        }
Ejemplo n.º 4
0
        public async Task SendReportPdfAsync(
            [QueueTrigger("sendreportpdf")] ReportQueue reportMessage,
            [Blob("report/{ReportId}.pdf", FileAccess.Write)] CloudBlockBlob reportPdfOutput,
#if DEBUG
            [Blob("report/{ReportId}.html", FileAccess.Write)] CloudBlockBlob reportHtmlOutput,
#endif
            CancellationToken cancellationToken)
        {
            try
            {
                Trace.CorrelationManager.ActivityId = Guid.NewGuid();
                logger.LogTrace($"SendReport trigger event recived, ID: {reportMessage?.ReportId}.");

                if (string.IsNullOrEmpty(reportMessage?.CultureName))
                {
                    throw new ArgumentNullException(nameof(ReportQueue.CultureName));
                }

                CultureHandler.SetCulture(reportMessage.CultureName);
                var translate = new Translate();

                var report = dbContext.Reports.Where(r => (r.Status == ReportStatus.Created || r.Status == ReportStatus.Resending) &&
                                                     r.PartitionId == reportMessage.PartitionId && r.Id == reportMessage.ReportId).FirstOrDefault();
                if (report == null)
                {
                    throw new Exception("Report do not exists or has invalid status and is therefore not send.");
                }
                var reportData = await report.ReportData.FromJson <ReportDataApi>();

                var organization = dbContext.Organizations.Where(o => o.Id == reportMessage.PartitionId).Select(o => new { o.Name, o.Address, o.Logo }).FirstOrDefault();

                using (var pdfReportHtmlStream = await ReportHtml.CreateReportStream(translate, reportData.ShowGroupColl, organization?.Logo, organization?.Name, organization?.Address, reportData.ReportTitle, reportData.ReportSubTitle, reportData.ReportText, reportData.Report))
                {
#if DEBUG
                    await reportHtmlOutput.UploadFromStreamAsync(pdfReportHtmlStream);

                    pdfReportHtmlStream.Position = 0;
#endif

                    using (var reportPdfStream = new MemoryStream())
                    {
                        logger.LogTrace($"Before create PDF: {reportMessage?.ReportId}.");
                        PdfProvider.CreatePdfAsync(reportPdfStream, pdfReportHtmlStream, cancellationToken: cancellationToken);
                        logger.LogTrace($"After create PDF: {reportMessage?.ReportId}.");

                        if (!cancellationToken.IsCancellationRequested)
                        {
                            await SendEmailReport(reportPdfStream, translate, report);
                            await UpdateReportStatus(reportMessage.PartitionId, reportMessage.ReportId, report.Status == ReportStatus.Created?ReportStatus.Send : ReportStatus.Resend);
                        }

                        await reportPdfOutput.UploadFromStreamAsync(reportPdfStream);
                    }
                }

                logger.LogTrace($"SendReport report send, ID: {reportMessage?.ReportId}.");
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"SendReport failed, ID: {reportMessage?.ReportId}.");
                throw;
            }
        }
        public void GetContentTest()
        {
            //Arrange
            var ns = new Mock <INodeServices>();

            var portableUser = new PortableUserDto
            {
                Name              = "Dennis",
                BirthDay          = DateTime.Now,
                CountryName       = "Denmark",
                SexType           = "Sex",
                BodyWeight        = 65,
                Email             = "*****@*****.**",
                RetrievalDate     = DateTime.Now,
                Height            = 178,
                ChallengeGiven    = 1,
                ChallengeReceived = 2,
                CreationDate      = DateTime.Now,
                DisplayName       = "LeetUser",
                Workouts          = new List <PortableWorkoutDto>
                {
                    new PortableWorkoutDto
                    {
                        CreationDate     = DateTime.Now,
                        Name             = "First ever",
                        WorkoutEntryDtos = new List <PortableWorkoutEntryDto>
                        {
                            new PortableWorkoutEntryDto
                            {
                                ExerciseName = "Benchpress",
                                Reps         = 12,
                                Set          = 2,
                                Weight       = 145
                            }
                        }
                    },
                    new PortableWorkoutDto
                    {
                        CreationDate     = DateTime.Now,
                        Name             = "Second",
                        WorkoutEntryDtos = new List <PortableWorkoutEntryDto>
                        {
                            new PortableWorkoutEntryDto
                            {
                                ExerciseName = "Squad",
                                Reps         = 12,
                                Set          = 1,
                                Weight       = 200
                            }
                        }
                    }
                }
            };

            ns.Setup(s => s.InvokeAsync <byte[]>(It.IsAny <string>(), It.IsAny <string>()))
            .ReturnsAsync(new byte[] { 0, 1, 0, 1, 1, 0 });

            var SUT = new PdfProvider(ns.Object);
            //Act
            var result = SUT.MakeFileAsync(portableUser).Result;

            //Assert
            Assert.NotNull(result);
        }
Ejemplo n.º 6
0
        public async Task SendInvoicePdfAsync(
            [QueueTrigger("sendinvoicepdf")] InvoiceQueue invoiceMessage,
            [Blob("invoice/{InvoiceId}.pdf", FileAccess.Write)] CloudBlockBlob invoicePdfOutput,
#if DEBUG
            [Blob("invoice/{InvoiceId}.html", FileAccess.Write)] CloudBlockBlob invoiceHtmlOutput,
#endif
            CancellationToken cancellationToken)
        {
            try
            {
                Trace.CorrelationManager.ActivityId = Guid.NewGuid();
                logger.LogTrace($"SendInvoice trigger event recived, ID: {invoiceMessage?.InvoiceId}.");

                if (string.IsNullOrEmpty(invoiceMessage?.CultureName))
                {
                    throw new ArgumentNullException(nameof(InvoiceQueue.CultureName));
                }

                CultureHandler.SetCulture(invoiceMessage.CultureName);
                var translate = new Translate();

                var invoice = dbContext.Invoices.Where(i => (i.Status == InvoiceStatus.Created || i.Status == InvoiceStatus.Resending) &&
                                                       i.PartitionId == invoiceMessage.PartitionId && i.Id == invoiceMessage.InvoiceId).FirstOrDefault();
                if (invoice == null)
                {
                    throw new Exception("Invoice do not exists or have invalid status and is therefore not send.");
                }
                var invoiceData = await invoice.InvoiceData.FromJson <InvoiceDataApi>();

                var organization = dbContext.Organizations.Where(o => o.Id == invoiceMessage.PartitionId).Select(o => new { o.Name, o.Address, o.Logo, o.VatNumber }).FirstOrDefault();

                using (var pdfInvoiceHtmlStream = await InvoiceHtml.CreateInvoiceStream(translate, invoice, invoiceData, organization?.Logo, organization?.Name, organization?.Address))
                {
#if DEBUG
                    await invoiceHtmlOutput.UploadFromStreamAsync(pdfInvoiceHtmlStream);

                    pdfInvoiceHtmlStream.Position = 0;
#endif

                    using (var invoicePdfStream = new MemoryStream())
                    {
                        logger.LogTrace($"Before create PDF: {invoiceMessage?.InvoiceId}.");
                        PdfProvider.CreatePdfAsync(invoicePdfStream, pdfInvoiceHtmlStream, cancellationToken: cancellationToken);
                        logger.LogTrace($"After create PDF: {invoiceMessage?.InvoiceId}.");

                        if (!cancellationToken.IsCancellationRequested)
                        {
                            await SendEmailInvoice(invoicePdfStream, translate, invoice);
                            await UpdateInvoiceStatus(invoiceMessage.PartitionId, invoiceMessage.InvoiceId, invoice.Status == InvoiceStatus.Created?InvoiceStatus.Send : InvoiceStatus.Resend);
                        }

                        await invoicePdfOutput.UploadFromStreamAsync(invoicePdfStream);
                    }
                }

                logger.LogTrace($"SendInvoice invoice send, ID: {invoiceMessage?.InvoiceId}.");
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"SendInvoice failed, ID: {invoiceMessage?.InvoiceId}.");
                throw;
            }
        }