Esempio n. 1
0
        public Error CreateDocumentPdf(Dictionary <string, string> headerProperties,
                                       List <Dictionary <string, string> > records,
                                       string templateFile,
                                       string outputFile,
                                       int maxItems = Int32.MaxValue)
        {
            var error = new Error();

            try {
                MediaService.MediaService mediaService = new MediaService.MediaService(db);

                string tempFile = MediaService.MediaService.GetTempFile(".html");

                // Perform the substitutions
                TemplateService.TemplateService ts = new TemplateService.TemplateService();

                var templateProperties = new TemplateProperties {
                    DocumentHeaderClass = "DocumentHeader",
                    PageHeaderClass     = "PageHeader",
                    ItemClass           = "ItemSection",
                    PageFooterClass     = "PageFooter",
                    DocumentFooterClass = "DocumentFooter"
                };

                error = ts.LoadTemplateFile(templateFile, templateProperties);

                if (!error.IsError)
                {
                    // Insert the lines
                    int maxItemsPerPage      = 0,
                        maxItemsBeforeFooter = ts.MaxItemsBeforeFooter,
                        pageNo      = 1,
                        itemsOnPage = 0;

                    headerProperties.AddProperty("PAGENO", pageNo);
                    ts.AddContent(templateProperties.DocumentHeaderClass, headerProperties);
                    ts.AddContent(templateProperties.PageHeaderClass, headerProperties);

                    // Add items
                    Dictionary <string, string> dict = new Dictionary <string, string>();

                    int itemCount = 1;
                    foreach (var line in records)
                    {
                        maxItemsPerPage = (pageNo == 1 ? ts.MaxItemsOnFirstPage : ts.MaxItemsOnSecondPage);
                        dict            = line;
                        if (itemsOnPage >= maxItemsPerPage)
                        {
                            headerProperties.AddProperty("PAGENO", pageNo);
                            ts.AddContent(templateProperties.PageFooterClass, headerProperties);      // Includes css page-break

                            pageNo++;
                            headerProperties.AddProperty("PAGENO", pageNo);
                            ts.AddContent(templateProperties.PageHeaderClass, headerProperties);
                            itemsOnPage = 0;
                        }

                        ts.AddContent(templateProperties.ItemClass, dict);
                        itemsOnPage++;
                        itemCount++;
                        if (itemCount > maxItems)
                        {
                            break;
                        }
                    }

                    // End of document
                    if (itemsOnPage > maxItemsBeforeFooter)
                    {
                        // Not enough room left on page, so break
                        ts.AddContent(templateProperties.PageFooterClass, headerProperties);      // Includes css page-break

                        pageNo++;
                        headerProperties.AddProperty("PAGENO", pageNo);
                        ts.AddContent(templateProperties.PageHeaderClass, headerProperties);
                        itemsOnPage = 0;
                    }

                    headerProperties.AddProperty("PAGECOUNT", pageNo);
                    ts.AddContent(templateProperties.DocumentFooterClass, headerProperties, dict);

                    // Write the html file
                    StreamWriter sw = new StreamWriter(tempFile);
                    sw.Write(ts.Render(headerProperties));
                    sw.Close();

                    // Create a folder for it
                    MediaService.MediaService.CreateDirectory(outputFile.FolderName());

                    // Convert it
                    error = convertHtmlFileToPDF(tempFile, outputFile);

                    MediaService.MediaService.DeleteFile(tempFile);
                    if (error.IsError)
                    {
                        MediaService.MediaService.DeleteFile(outputFile);
                    }
                }
            } catch (Exception e1) {
                error.SetError(e1);
            }

            return(error);
        }
        public void LoadTemplateFileTest()
        {
            var templateProperties = new TemplateProperties {
                PageHeaderClass     = "PageHeader",
                ItemClass           = "ItemSection",
                PageFooterClass     = "PageFooter",
                DocumentFooterClass = "DocumentFooter"
            };

            TemplateService.TemplateService ts = new TemplateService.TemplateService();

            string templateFile = GetAppSetting("SiteFolder", "") + @"\App_Data\PurchaseOrderTemplates\PurchaseOrder-Default.html";
            var    error        = ts.LoadTemplateFile(templateFile, templateProperties);


            // Build the document
            int pageNo      = 1,
                itemsOnPage = 0;
            Dictionary <string, string> dict = new Dictionary <string, string>();

            ts.AddContent("PageHeader", null);

            // Add some items
            for (int i = 1; i <= 50; i++)
            {
                dict = new Dictionary <string, string>();
                dict.AddProperty("ORDERQTY", i);
                dict.AddProperty("SUPPLIERITEMNUMBER", i);
                dict.AddProperty("OURPRODCODE", i);
                dict.AddProperty("DESCRIPTION", i);
                dict.AddProperty("UNITPRICEEXTAX", i);
                dict.AddProperty("DISCOUNTPERCENT", i);
                dict.AddProperty("LINEPRICE", i);

                if (itemsOnPage >= 39)
                {
                    dict.AddProperty("PAGENO", pageNo);
                    ts.AddContent("PageFooter", dict);      // Includes css page-break

                    pageNo++;
                    dict.AddProperty("PAGENO", pageNo);
                    ts.AddContent("PageHeader", dict);
                    itemsOnPage = 0;
                }

                ts.AddContent("ItemSection", dict);
                itemsOnPage++;
            }

            // End of document
            dict = new Dictionary <string, string>();
            dict.AddProperty("PAGENO", pageNo);

            if (itemsOnPage > 20)
            {
                // Not enough room left on page, so break
                ts.AddContent("PageFooter", dict);      // Includes css page-break

                pageNo++;
                dict.AddProperty("PAGENO", pageNo);
                ts.AddContent("PageHeader", dict);
                itemsOnPage = 0;
            }

            dict = new Dictionary <string, string>();
            dict.AddProperty("PAGENO", pageNo);

            ts.AddContent("DocumentFooter", dict);

            // Get the document
            string docText = ts.Render(dict);

            //using (StreamWriter sw = new StreamWriter(@"c:\temp\Debug.html")) {
            //    sw.Write(ts.Render());
            //}

            string macro = "ORDERQTY";

            Assert.IsTrue(docText.IndexOf(macro) == -1, $"Error: Template macro {macro} was found when it was expected to be substituted");
            macro = "SUPPLIERITEMNUMBER";
            Assert.IsTrue(docText.IndexOf(macro) == -1, $"Error: Template macro {macro} was found when it was expected to be substituted");
            macro = "OURPRODCODE";
            Assert.IsTrue(docText.IndexOf(macro) == -1, $"Error: Template macro {macro} was found when it was expected to be substituted");
            macro = "DESCRIPTION";
            Assert.IsTrue(docText.IndexOf(macro) == -1, $"Error: Template macro {macro} was found when it was expected to be substituted");
            macro = "UNITPRICEEXTAX";
            Assert.IsTrue(docText.IndexOf(macro) == -1, $"Error: Template macro {macro} was found when it was expected to be substituted");
            macro = "DISCOUNTPERCENT";
            Assert.IsTrue(docText.IndexOf(macro) == -1, $"Error: Template macro {macro} was found when it was expected to be substituted");
            macro = "LINEPRICE";
            Assert.IsTrue(docText.IndexOf(macro) == -1, $"Error: Template macro {macro} was found when it was expected to be substituted");
        }