Exemplo n.º 1
0
        /// <summary>
        /// This function is used for creating the invoice based on cshtml templates
        /// </summary>
        /// <param name="documentTemplate"></param>
        /// <param name="filePathToSave"></param>
        /// <param name="templateOption"></param>
        /// <returns></returns>
        public bool CreatePDFBasedOnCsHtmlTemplate(DocumentTemplate documentTemplate, string filePathToSave, TemplateOption templateOption = null)
        {
            FileStream objFileStream = null;

            try
            {
                string headerHtml = string.Empty;
                string bodyHtml   = string.Empty;
                string footerHtml = string.Empty;


                documentTemplate.InvoicePdfGenerationDetails = new InvoicePdfGenerationDetails()
                {
                    ImagesFolderRootPath = ""
                };

                if (!string.IsNullOrWhiteSpace(documentTemplate.HeaderCsHtml))
                {
                    // Getting the header html
                    headerHtml = RazorBoss.GetHtmlFromRazorView(documentTemplate.HeaderCsHtml, documentTemplate.InvoicePdfGenerationDetails);
                }

                if (!string.IsNullOrWhiteSpace(documentTemplate.BodyCsHtml))
                {
                    // Getting the body Html
                    bodyHtml = RazorBoss.GetHtmlFromRazorView(documentTemplate.BodyCsHtml, documentTemplate.InvoicePdfGenerationDetails);
                }

                if (!string.IsNullOrWhiteSpace(documentTemplate.FooterCsHtml))
                {
                    // Getting the footer html
                    footerHtml = RazorBoss.GetHtmlFromRazorView(documentTemplate.FooterCsHtml, documentTemplate.InvoicePdfGenerationDetails);
                }

                // Getting the blob data based on generated html and template option for file creation
                byte[] blob = CreatePDFBasedOnHTMLTemplate(headerHtml, bodyHtml, footerHtml, templateOption);

                // Creating the file stream object and writing to the file
                objFileStream = new FileStream(filePathToSave, FileMode.Create);
                objFileStream.Write(blob, 0, blob.Length);

                return(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.InnerException.Message);
                throw;
            }
            finally
            {
                // Closing and disposing the file stream object
                if (objFileStream != null)
                {
                    objFileStream.Close();
                    objFileStream.Dispose();
                }
            }
        }
Exemplo n.º 2
0
        public bool CreateHtmlBasedOnCsHtmlTemplate(DocumentTemplate documentTemplate, string filePathToSave, TemplateOption templateOption = null)
        {
            FileStream objFileStream = null;

            try
            {
                string headerHtml = string.Empty;
                string bodyHtml   = string.Empty;
                string footerHtml = string.Empty;


                documentTemplate.InvoicePdfGenerationDetails = new InvoicePdfGenerationDetails()
                {
                    ImagesFolderRootPath = ""
                };

                if (!string.IsNullOrWhiteSpace(documentTemplate.HeaderCsHtml))
                {
                    // Getting the header html
                    headerHtml = RazorBoss.GetHtmlFromRazorView(documentTemplate.HeaderCsHtml, documentTemplate.InvoicePdfGenerationDetails);
                }

                if (!string.IsNullOrWhiteSpace(documentTemplate.BodyCsHtml))
                {
                    // Getting the body Html
                    bodyHtml = RazorBoss.GetHtmlFromRazorView(documentTemplate.BodyCsHtml, documentTemplate.InvoicePdfGenerationDetails);
                }

                if (!string.IsNullOrWhiteSpace(documentTemplate.FooterCsHtml))
                {
                    // Getting the footer html
                    footerHtml = RazorBoss.GetHtmlFromRazorView(documentTemplate.FooterCsHtml, documentTemplate.InvoicePdfGenerationDetails);
                }

                // We write out html here :
                StringBuilder buffer = new StringBuilder();
                buffer.Append("<Html><head></head><body>");
                buffer.Append(headerHtml);
                buffer.Append(bodyHtml);
                buffer.Append(footerHtml);
                buffer.Append("</body></Html>");
                // we get html
                byte[] blob = Encoding.ASCII.GetBytes(buffer.ToString());



                // Creating the file stream object and writing to the file
                objFileStream = new FileStream(filePathToSave, FileMode.Create);
                objFileStream.Write(blob, 0, blob.Length);

                return(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.InnerException.Message);
                throw;
            }
            finally
            {
                // Closing and disposing the file stream object
                if (objFileStream != null)
                {
                    objFileStream.Close();
                    objFileStream.Dispose();
                }
            }
        }