Пример #1
0
    protected void Page_Load(object sender, EventArgs e)
    {
        Stream template = null;
        Stream data     = null;
        Report proc;

        try
        {
            // get the report files
            string templateFile = Request.PhysicalApplicationPath + "files\\Example_Template.docx";
            template = new FileStream(templateFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            // create the report
            string contentType;
            switch ((int)Session["report"])
            {
            case 0:
                contentType = "application/pdf";
                proc        = new ReportPdf(template);
                break;

            case 1:
                contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                proc        = new ReportDocx(template);
                break;

            case 2:
                contentType = "text/html";
                proc        = new ReportHtml(template);
                ((ReportHtml)proc).SetImagePath(Request.PhysicalApplicationPath + "images", "images", "wr_");
                break;

            default:
                lblResult.Text = "Error: unknown report type " + Session["report"];
                return;
            }
            proc.ProcessSetup();

            // set variables
            Dictionary <string, object> map = new Dictionary <string, object>();
            map.Add("LeaveRequestId", Session["var"]);
            map.Add("CSRName", "John Brown");

            // apply data
            data = new FileStream(Request.PhysicalApplicationPath + "files\\Example_Data.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            IReportDataSource ds = new XmlDataSourceImpl(data, false);
            proc.Parameters = map;
            proc.ProcessData(ds, "");
            ds.Close();

            proc.ProcessComplete();

            // get the output and display it
            Response.ContentType = contentType;
            Response.BinaryWrite(((MemoryStream)proc.GetReport()).ToArray());
            proc.Close();
        }

        catch (Exception ex)
        {
            lblResult.Text = ex.Message;
            return;
        }
        // close everything
        finally
        {
            if (template != null)
            {
                template.Close();
            }
            if (data != null)
            {
                data.Close();
            }
        }

        // this will throw an exception so we have it here at the end - must be last line of code!
        Response.End();
    }
Пример #2
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;
            }
        }