예제 #1
0
        public ActionResult GeneratePDFOutput()
        {
            // Get BookID, VersionID & Year from Cookie
            var pContext    = System.Web.HttpContext.Current;
            var lCookieData = CookieManagement.GetCookie(pContext);
            var lLogMsg     = string.Empty;
            var pdfService  = new PDFService();

            try
            {
                // Print Output for given BookID
                var pdfModel = new PDFDocumentModel
                {
                    Book        = lCookieData.Book,
                    BookID      = Convert.ToInt32(lCookieData.BookID),
                    Publisher   = lCookieData.Publisher,
                    PublisherID = Convert.ToInt32(lCookieData.PublisherID),
                    VersionID   = lCookieData.VersionID,
                    Year        = lCookieData.Year
                };

                var lResult = pdfService.BuildPDF(pdfModel, pContext);

                if (lResult.Any())
                {
                    // Set Up the Response Stream for the Spec Sheet Collection
                    System.Web.HttpContext.Current.Response.Clear();
                    System.Web.HttpContext.Current.Response.BufferOutput = false;
                    System.Web.HttpContext.Current.Response.ContentType  = "application/pdf";
                    System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + "loreal_output.pdf");
                    // Create a FileStream for the PDF and Send it to the Response's OutputStream
                    using (var outStream = new FileStream(pdfService.FilePath, FileMode.Open))
                    {
                        byte[] buffer = new byte[4096];
                        int    count  = 0;
                        while ((count = outStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            System.Web.HttpContext.Current.Response.OutputStream.Write(buffer, 0, count);
                            System.Web.HttpContext.Current.Response.Flush();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                var lReturnMessage = String.Format("Error in PrintPDFOutput.  BOOK: {0}, Publisher: {1}.", lCookieData.Book, lCookieData.Publisher);
                lLogMsg = String.Format(lReturnMessage + "ERROR MESSAGE: {0}.  SOURCE: {1}. STACKTRACE: {2}.", ex.Message, ex.Source, ex.StackTrace);
                logger.Error(lLogMsg);
            }

            return(View());
        }
예제 #2
0
        //public HttpResponseMessage PrintPDFOutput()
        //public System.Web.Mvc.ActionResult PrintPDFOutput()
        public IHttpActionResult PrintPDFOutput()
        {
            // Get BookID, VersionID & Year from Cookie
            var pContext    = System.Web.HttpContext.Current;
            var lCookieData = CookieManagement.GetCookie(pContext);
            var lLogMsg     = string.Empty;
            var pdfService  = new PDFService();

            try
            {
                // Print Output for given BookID
                var pdfModel = new PDFDocumentModel
                {
                    Book        = lCookieData.Book,
                    BookID      = Convert.ToInt32(lCookieData.BookID),
                    Publisher   = lCookieData.Publisher,
                    PublisherID = Convert.ToInt32(lCookieData.PublisherID),
                    VersionID   = lCookieData.VersionID,
                    Year        = lCookieData.Year
                };

                var lResult = pdfService.BuildPDF(pdfModel, pContext);

                if (lResult.Any())
                {
                    // Set up the Response Stream for the File
                    HttpContext.Current.Response.Clear();
                    HttpContext.Current.Response.ClearHeaders();
                    HttpContext.Current.Response.ClearContent();
                    HttpContext.Current.Response.Expires = -1000;
                    //HttpContext.Current.Response.BufferOutput = false;
                    HttpContext.Current.Response.ContentType = "application/pdf";
                    HttpContext.Current.Response.AddHeader("content-length", lResult.Length.ToString());
                    HttpContext.Current.Response.AddHeader("content-disposition", "inline; filename=" + "loreal_output.pdf");
                    HttpContext.Current.Response.BinaryWrite(lResult);
                    HttpContext.Current.Response.End();
                }

                //}
            }
            catch (Exception ex)
            {
                var lReturnMessage = String.Format("Error in PrintPDFOutput.  BOOK: {0}, Publisher: {1}.", lCookieData.Book, lCookieData.Publisher);
                lLogMsg = String.Format(lReturnMessage + "ERROR MESSAGE: {0}.  SOURCE: {1}. STACKTRACE: {2}.", ex.Message, ex.Source, ex.StackTrace);
                logger.Error(lLogMsg);
            }

            return(Ok(HttpContext.Current.Response));
        }