Example #1
0
        public ActionResult PdfPrintDoc(Guid?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Tournament tournament = db.Tournaments.Find(id);

            var printUrl = System.Configuration.ConfigurationManager.AppSettings["html2pdf"];

            if (string.IsNullOrEmpty(printUrl))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "html2pdf must be added to configuration"));
            }

            var printCommand = new PrintCommand()
            {
                Name = tournament.Name
            };

            if (CheckHasDivisions(tournament, true, 1, 8))
            {
                printCommand.Documents.Add(new Subdocument()
                {
                    Address = Url.AbsoluteAction("PrintSubdocument", "Tournaments",
                                                 new { id = id, drawBracket = true, minSize = 1, maxSize = 8, pixHeight = 500 }),
                    Orientation = "Landscape",
                    MarginAll   = 1,
                    PageSize    = "Letter",
                    ZoomFactor  = 1.5
                });
            }
            if (CheckHasDivisions(tournament, true, 9, 16))
            {
                printCommand.Documents.Add(new Subdocument()
                {
                    Address = Url.AbsoluteAction("PrintSubdocument", "Tournaments",
                                                 new { id = id, drawBracket = true, minSize = 9, maxSize = 16, pixHeight = 850 }),
                    Orientation = "Landscape",
                    MarginAll   = 1,
                    PageSize    = "Letter",
                    ZoomFactor  = 0.95
                });
            }
            if (CheckHasDivisions(tournament, true, 17, 32))
            {
                printCommand.Documents.Add(new Subdocument()
                {
                    Address = Url.AbsoluteAction("PrintSubdocument", "Tournaments",
                                                 new { id = id, drawBracket = true, minSize = 17, maxSize = 32, pixHeight = 1500 }),
                    Orientation = "Portrait",
                    MarginAll   = 2,
                    PageSize    = "Letter",
                    ZoomFactor  = 0.7
                });
            }
            if (CheckHasDivisions(tournament, true, 33, 64))
            {
                printCommand.Documents.Add(new Subdocument()
                {
                    Address = Url.AbsoluteAction("PrintSubdocument", "Tournaments",
                                                 new { id = id, drawBracket = true, minSize = 33, maxSize = 64, pixHeight = 3000 }),
                    Orientation = "Portrait",
                    MarginAll   = 1.5,
                    PageSize    = "A2",
                    ZoomFactor  = 0.82
                });
            }
            if (CheckHasDivisions(tournament, false, 1, 32))
            {
                printCommand.Documents.Add(new Subdocument()
                {
                    Address = Url.AbsoluteAction("PrintSubdocument", "Tournaments",
                                                 new { id = id, drawBracket = false, minSize = 1, maxSize = 32, pixHeight = 840 }),
                    Orientation = "Landscape",
                    MarginAll   = 1,
                    PageSize    = "Letter",
                    ZoomFactor  = 0.95
                });
            }
            if (CheckHasDivisions(tournament, false, 33, 64))
            {
                printCommand.Documents.Add(new Subdocument()
                {
                    Address = Url.AbsoluteAction("PrintSubdocument", "Tournaments",
                                                 new { id = id, drawBracket = false, minSize = 33, maxSize = 64, pixHeight = 1100 }),
                    Orientation = "Portrait",
                    MarginAll   = 2,
                    PageSize    = "Letter",
                    ZoomFactor  = 0.90
                });
            }

            var    javaScriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            string jsonString           = javaScriptSerializer.Serialize(printCommand);

            using (var client = new ExtendedTimeout())
            {
                client.Headers[HttpRequestHeader.ContentType] = "application/json";
                byte[] result = client.UploadData(printUrl, (new ASCIIEncoding()).GetBytes(jsonString));
                return(new FileContentResult(result, "application/pdf")
                {
                    FileDownloadName = MakeValidFileName(tournament.Name) + ".pdf"
                });
            }
        }
Example #2
0
        //divisionId is null - prints them all
        public ActionResult PdfPrintDivision(Guid?id, Guid?divisionId)
        {
            int divisionNumber = 0;
            var templates      = new PrintTemplate[]
            {
                new PrintTemplate()
                {
                    min = 4, max = 4, hasBracket = true, ignoreConsolidationRoundSetting = false, consolidationRound = true, orientation = "Landscape", pageSize = "Letter", pagePixHeight = 240, marginAll = 1, zoomFactor = 1.5
                },
                new PrintTemplate()
                {
                    min = 1, max = 8, hasBracket = true, ignoreConsolidationRoundSetting = true, orientation = "Landscape", pageSize = "Letter", pagePixHeight = 720, marginAll = 1, zoomFactor = 1.5
                },
                new PrintTemplate()
                {
                    min = 9, max = 16, hasBracket = true, ignoreConsolidationRoundSetting = true, orientation = "Portrait", pageSize = "Letter", pagePixHeight = 1340, marginAll = 0.5, zoomFactor = .95
                },
                new PrintTemplate()
                {
                    min = 17, max = 32, hasBracket = true, ignoreConsolidationRoundSetting = true, orientation = "Portrait", pageSize = "Letter", pagePixHeight = 1500, marginAll = 2, zoomFactor = .7
                },
                new PrintTemplate()
                {
                    min = 33, max = 64, hasBracket = true, ignoreConsolidationRoundSetting = true, orientation = "Portrait", pageSize = "A2", pagePixHeight = 3000, marginAll = 1.5, zoomFactor = .82
                },
                new PrintTemplate()
                {
                    min = 1, max = 32, hasBracket = false, ignoreConsolidationRoundSetting = true, orientation = "Landscape", pageSize = "Letter", pagePixHeight = 840, marginAll = 1, zoomFactor = .95
                },
                new PrintTemplate()
                {
                    min = 33, max = 128, hasBracket = false, ignoreConsolidationRoundSetting = true, orientation = "Portrait", pageSize = "Letter", pagePixHeight = 1200, marginAll = 1, zoomFactor = .90
                }
            };

            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Tournament tournament = db.Tournaments.Find(id);

            var printUrl = System.Configuration.ConfigurationManager.AppSettings["html2pdf"];

            if (string.IsNullOrEmpty(printUrl))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "html2pdf must be added to configuration"));
            }

            var printCommand = new PrintCommand()
            {
                Name = tournament.Name
            };

            foreach (var division in tournament.Divisions.Where(d => (divisionId == null || d.DivisionId == divisionId)).OrderBy(d => d.OrderId))
            {
                if (divisionId != null)
                {
                    divisionNumber = (int)division.OrderId;
                }

                //hack - I treat divisions of 1 as flat therefore I need to apply the right rule here
                if (division.ParticipantDivisionInts.Count == 1)
                {
                    var template = templates[5];//hack to get flat of 1
                    printCommand.Documents.Add(new Subdocument()
                    {
                        Address = Url.AbsoluteAction("PrintSingleDocument", "Tournaments",
                                                     new { id = id, divisionId = division.DivisionId, pixHeight = template.pagePixHeight }),
                        Orientation = template.orientation,
                        MarginAll   = template.marginAll,
                        PageSize    = template.pageSize,
                        ZoomFactor  = template.zoomFactor
                    });
                }
                else
                {
                    foreach (var template in templates)
                    {
                        if (template.hasBracket == division.DrawBracket && (template.ignoreConsolidationRoundSetting || template.consolidationRound == division.ConsolidationRound) &&
                            template.min <= division.ParticipantDivisionInts.Count && template.max >= division.ParticipantDivisionInts.Count)
                        {
                            printCommand.Documents.Add(new Subdocument()
                            {
                                Address = division.ParticipantDivisionInts.Count <= 16 ? Url.AbsoluteAction("PrintSingleDocumentSmallDivision", "Tournaments",
                                                                                                            new { id = id, divisionId = division.DivisionId, pixHeight = template.pagePixHeight }) : Url.AbsoluteAction("PrintSingleDocument", "Tournaments",
                                                                                                                                                                                                                        new { id = id, divisionId = division.DivisionId, pixHeight = template.pagePixHeight }),
                                Orientation = /*(division.ParticipantDivisionInts.Count == 4 && division.ConsolidationRound) ? "Portrait" : */ template.orientation,
                                MarginAll   = template.marginAll,
                                PageSize    = template.pageSize,
                                ZoomFactor  = template.zoomFactor
                            });
                            break;
                        }
                    }
                }
            }

            var    javaScriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            string jsonString           = javaScriptSerializer.Serialize(printCommand);

            using (var client = new ExtendedTimeout())
            {
                client.Headers[HttpRequestHeader.ContentType] = "application/json";
                byte[] result = client.UploadData(printUrl, (new ASCIIEncoding()).GetBytes(jsonString));
                return(new FileContentResult(result, "application/pdf")
                {
                    FileDownloadName = MakeValidFileName(tournament.Name) + ((divisionId == null) ? "" : string.Format("_division_{0}", divisionNumber)) + ".pdf"
                });
            }
        }