コード例 #1
0
        public TemplateResponse Render(RenderingOptions options)
        {
            if (options == null)
            {
                throw new FaultException <string>("Rendering options are null");
            }

            var _param = new RenderingParameters();

            if (options.OutputFormat == OutputFormat.PDF)
            {
                _param.OutputFormat   = Engine.OutputFormat.PDF;
                _param.PDFOutputFlags = Engine.PDFFlags.PDF14;
            }
            else if (options.OutputFormat == OutputFormat.HTML)
            {
                _param.OutputFormat       = Engine.OutputFormat.HTML;
                _param.HtmlOutputSettings = new Engine.HtmlOutputSettings
                {
                    GenerateHtmlDocument = false
                };
            }
            else
            {
                // no other format is supported...
                throw new FaultException <string>("Rendering.OutputFormat not supported");
            }

            var template = XDocument.Parse(options.Template);

            Stream stream = new MemoryStream();

            template.Save(stream);
            // Rewind the stream ready to read from it elsewhere
            stream.Position = 0;

            _param.Template = new LocalDocumentTemplate(stream, null, Engine.XsltEngine.MSXML, "en_EN", null);
            var _output = new OutputInformation();
            var ds      = DataSource(options);

            using (var outputStream = new MemoryStream())
            {
                Engine engine = new Engine();
                engine.Render(ds, outputStream, _param, ref _output);
                return(new TemplateResponse
                {
                    Response = outputStream.ToArray()
                });
            }
        }
コード例 #2
0
ファイル: Templater.svc.cs プロジェクト: hahmed/tr
        public TemplateResponse Render(RenderingOptions options)
        {
            if (options == null)
            {
                throw new FaultException<string>("Rendering options are null");
            }

            var _param = new RenderingParameters();

            if (options.OutputFormat == OutputFormat.PDF)
            {
                _param.OutputFormat = Engine.OutputFormat.PDF;
                _param.PDFOutputFlags = Engine.PDFFlags.PDF14;
            }
            else if (options.OutputFormat == OutputFormat.HTML)
            {
                _param.OutputFormat = Engine.OutputFormat.HTML;
                _param.HtmlOutputSettings = new Engine.HtmlOutputSettings
                {
                     GenerateHtmlDocument = false
                };
            }
            else
            {
                // no other format is supported...
                throw new FaultException<string>("Rendering.OutputFormat not supported");
            }

            var template = XDocument.Parse(options.Template);

            Stream stream = new MemoryStream();
            template.Save(stream);
            // Rewind the stream ready to read from it elsewhere
            stream.Position = 0;

            _param.Template = new LocalDocumentTemplate(stream, null, Engine.XsltEngine.MSXML, "en_EN", null);
            var _output = new OutputInformation();
            var ds = DataSource(options);

            using (var outputStream = new MemoryStream())
            {
                Engine engine = new Engine();
                engine.Render(ds, outputStream, _param, ref _output);
                return new TemplateResponse
                {
                    Response = outputStream.ToArray()
                };
            }
        }
コード例 #3
0
        /// <summary>
        /// This method will generate Pdf file from HtmlDataSource property and will use
        /// RenderingParameters as pdf file rendering parameters
        /// </summary>
        /// <param name="applicationId">Form applicatio id</param>
        /// <param name="html">Html content to crete pdf</param>
        public void GeneratePdf(int applicationId, string html)
        {
            this.HtmlDataSource = html;
            this.PdfFileLocationPath = HostingEnvironment.MapPath(@"~/Content/pdfs");

            if (string.IsNullOrEmpty(this.PdfFileLocationPath))
            {
                throw new ArgumentException("Specify pdf file saving location");
            }

            string outFilePath = Path.Combine(this.PdfFileLocationPath, applicationId.ToString());

            #if DEBUG
            // For testing purpose save html
            File.WriteAllText(outFilePath + ".html", html);
            #endif

            using (FileStream outputStream = new FileStream(outFilePath + ".pdf", FileMode.Create))
            {
                OutputInformation outInfo = new OutputInformation();
                var processInfo = new PdfCreateProcessInfo();
                processInfo.OnMassageArrived += this.ProcessInfo_OnMassageArrived;

                outInfo.NotificationEvents = processInfo;
                var pdfEngine = new Engine();

                pdfEngine.Render(this, outputStream, this.RenderingParameters, ref outInfo);

                if (!this.IsError)
                {
                    Attachment att = new Attachment
                    {
                        ApplicationFormId = applicationId,
                        AttachmentType = DbObjects.OLE.TableRefEnums.AttachmentType.PdfApplication,
                        Description = "pdf document",
                        FileName = applicationId.ToString(),
                        ServerFileName = "~/Content/pdfs/" + applicationId.ToString() + ".pdf"

                    };
                    this.databaseHelper.Create<Attachment>(att);
                    this.databaseHelper.FlushChanges();
                }
            }
        }