예제 #1
0
 public StandaloneGenerator(IParser parser, string imageBasePath, Margins margins)
 {
     Parser        = parser;
     ImageBasePath = imageBasePath;
     Margins       = margins;
 }
예제 #2
0
 public MvcGenerator(ControllerContext controllerContext, Margins margins)
 {
     ControllerContext = controllerContext;
     Margins           = margins;
 }
        private byte[] InternalGeneratePdf <T>(Action <PdfWriter, Document> configureSettings, T model = null, string viewName = null, List <BasePageSnippet> pageSnippets = null, Margins margins = null) where T : class
        {
            byte[] output;

            var document = margins == null
                ? new Document()
                : new Document(margins.PageSize, margins.Left, margins.Right, margins.Top, margins.Bottom);

            using (document)
            {
                using (var workStream = new MemoryStream())
                {
                    var writer = PdfWriter.GetInstance(document, workStream);
                    writer.CloseStream = false;

                    if (pageSnippets != null)
                    {
                        var aggregateHelper = new AggregateHelper(pageSnippets, InternalGeneratePdf);
                        writer.PageEvent = aggregateHelper;
                    }

                    configureSettings?.Invoke(writer, document);
                    document.Open();

                    var renderedView = Parser.RenderView(viewName, model, Templates);

                    using (var reader = new StringReader(renderedView))
                    {
                        var workerInstance = XMLWorkerHelper.GetInstance();

                        var tagProcessors = Tags.GetHtmlTagProcessorFactory();
                        if (!string.IsNullOrEmpty(ImageBasePath))
                        {
                            tagProcessors.RemoveProcessor(HTML.Tag.IMG);
                            tagProcessors.AddProcessor(new ImageTagProcessor(ImageBasePath), new[] { HTML.Tag.IMG });
                        }

                        var htmlContext = new HtmlPipelineContext(null);

                        htmlContext.SetTagFactory(tagProcessors);

                        var cssResolver = workerInstance.GetDefaultCssResolver(true);
                        var pipeline    = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer)));

                        var worker = new XMLWorker(pipeline, true);
                        var parser = new XMLParser(worker);
                        parser.Parse(reader);

                        document.Close();
                        output = workStream.ToArray();
                    }
                }
            }
            return(output);
        }
예제 #4
0
 public MvcGenerator(ControllerContext controllerContext, string imageBasePath, Margins margins)
 {
     ControllerContext = controllerContext;
     ImageBasePath     = imageBasePath;
     Margins           = margins;
 }