public ActionResult HandblebarPdf(HandlebarPdfRequestEntity model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                var html   = HandlebarHelper.DataTemplateToHtml(model.HandlebarHtmlTemplate, model.JsonData);
                var entity = new HandlebarPdfEntity
                {
                    Header              = model.Header,
                    Html                = html,
                    FileSavePath        = model.FileSavePath,
                    UniqueReference     = model.UniqueReference,
                    AdditionalDocuments = model.AdditionalDocuments
                };

                var response = PdfCreationService.CreatePdf(entity);

                return(Ok(response));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex));
            }
        }
        public void AddAdditionalDocument_ReturnsBytes()
        {
            var entity = new HandlebarPdfEntity
            {
                Html            = "<h1>Test</h1>",
                FileSavePath    = "Test",
                Header          = "Test",
                UniqueReference = "Test"
            };

            var initialPdfResponse = _service.CreateInitialPdf(entity);

            var imageString = BaseStrings.ReturnImageString();

            var additionalDocuments = new List <AdditionalDocuments>
            {
                new AdditionalDocuments
                {
                    Base64Document = imageString,
                    Type           = AdditionalDocumentTypes.Jpeg
                },
                new AdditionalDocuments
                {
                    Base64Document = imageString,
                    Type           = AdditionalDocumentTypes.Jpg
                },
                new AdditionalDocuments
                {
                    Base64Document = imageString,
                    Type           = AdditionalDocumentTypes.Png
                },
                new AdditionalDocuments
                {
                    Base64Document = BaseStrings.ReturnPdfString(),
                    Type           = AdditionalDocumentTypes.Pdf
                },
                new AdditionalDocuments
                {
                    Base64Document = BaseStrings.ReturnHtmlString(),
                    Type           = AdditionalDocumentTypes.Html
                }
            };

            var response = _service.AddAdditionalDocument(initialPdfResponse, additionalDocuments);

            Assert.IsType <byte[]>(initialPdfResponse);
            Assert.NotEmpty(initialPdfResponse);

            Assert.IsType <byte[]>(response);
            Assert.NotEmpty(response);
        }
        public void CreateInitialPdf_ReturnsBytes()
        {
            var entity = new HandlebarPdfEntity
            {
                Html            = "<h1>Test</h1>",
                FileSavePath    = "Test",
                Header          = "Test",
                UniqueReference = "Test"
            };

            var response = _service.CreateInitialPdf(entity);

            Assert.IsType <byte[]>(response);
            Assert.NotEmpty(response);
        }
Ejemplo n.º 4
0
        public byte[] CreateInitialPdf(HandlebarPdfEntity model)
        {
            var memoryStream = new MemoryStream();
            var document     = new Document();
            var writer       = PdfWriter.GetInstance(document, memoryStream);

            document.Open();

            writer.PageEvent = new CreateFooterAndHeaderEvent(model.Header, DateTime.Now.ToString("d"), model.UniqueReference);

            var htmlWorker = new HTMLWorker(document);
            var sr         = new StringReader(model.Html);
            var styles     = new StyleSheet();

            styles.LoadStyle("li", "list-style-type", "circle");

            htmlWorker.SetStyleSheet(styles);
            htmlWorker.Parse(sr);

            document.Close();

            return(memoryStream.ToArray());
        }
Ejemplo n.º 5
0
        public string CreatePdf(HandlebarPdfEntity model)
        {
            var bytes = CreateInitialPdf(model);

            var dirPath = model.FileSavePath + "/" + model.EscapedUniqueReference;

            Directory.CreateDirectory(dirPath);

            if (model.AdditionalDocuments != null)
            {
                SplitAdditionalDocuments(model.AdditionalDocuments);
                bytes = AddAdditionalDocument(bytes, model.AdditionalDocuments);

                if (WordList != null)
                {
                    CreateWordDocuments(dirPath, model.EscapedUniqueReference);
                }
            }

            FileHelper.StreamToFile(dirPath, model.EscapedUniqueReference, "pdf", new MemoryStream(bytes));

            return(dirPath);
        }