public static byte[] Render(string html, List <string> cssFiles = null, Rectangle pageSize = null)
        {
            if (pageSize == null)
            {
                pageSize = PageSize.A4.Rotate();
            }
            using (var stream = new MemoryStream())
            {
                // create an iTextSharp Document which is an abstraction of a PDF but **NOT** a PDF
                using (var document = new Document(pageSize))
                {
                    // create a writer that's bound to our PDF abstraction and our stream
                    using (var writer = PdfWriter.GetInstance(document, stream))
                    {
                        // open the document for writing
                        document.Open();
                        HtmlPipelineContext  htmlContext = new HtmlPipelineContext(null);
                        ITagProcessorFactory factory     = Tags.GetHtmlTagProcessorFactory();
                        factory.AddProcessor(new CustomImageHTMLTagProcessor(), new string[] { "img" });
                        htmlContext.SetTagFactory(factory);

                        var isAnyCssFiles = cssFiles != null && cssFiles.Count > 0;
                        //create a cssresolver to apply css
                        ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(!isAnyCssFiles);
                        if (isAnyCssFiles)
                        {
                            foreach (var cssfile in cssFiles)
                            {
                                if (cssfile.StartsWith("http"))
                                {
                                    cssResolver.AddCssFile(cssfile, true);
                                }
                                else
                                {
                                    cssResolver.AddCssFile(System.Web.HttpContext.Current.Server.MapPath(cssfile), true);
                                }
                            }
                        }
                        //create and attach pipeline
                        IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer)));

                        XMLWorker worker    = new XMLWorker(pipeline, true);
                        XMLParser xmlParser = new XMLParser(true, worker);
                        using (var srHtml = new StringReader(html))
                        {
                            xmlParser.Parse(srHtml);
                        }

                        // close document
                        document.Close();
                    }
                }

                // get bytes from stream
                byte[] bytes = stream.ToArray();
                bytes = AddPageNumbers(bytes, pageSize);
                // success
                return(bytes);
            }
        }
 virtual protected internal ITagProcessorFactory GetDefaultTagProcessorFactory()
 {
     if (null == tpf)
     {
         tpf = Tags.GetHtmlTagProcessorFactory();
     }
     return(tpf);
 }
Exemple #3
0
 protected ITagProcessorFactory GetDefaultTagProcessorFactory()
 {
     if (null == tpf)
     {
         tpf = SvgTags.GetSvgTagProcessorFactory();
     }
     return(tpf);
 }
 /**
  * Retrieves the default factory for processing HTML tags from
  * {@link Tags#getHtmlTagProcessorFactory()}. On subsequent calls the same
  * {@link TagProcessorFactory} is returned every time. <br />
  * @return a
  *         <code>DefaultTagProcessorFactory<code> that maps HTML tags to {@link TagProcessor}s
  */
 protected internal ITagProcessorFactory GetDefaultTagProcessorFactory() {
     if (null == tpf) {
         tpf = Tags.GetHtmlTagProcessorFactory();
     }
     return tpf;
 }
 /**
  * Set the {@link TagProcessorFactory} to be used. For HTML use {@link Tags#getHtmlTagProcessorFactory()}
  * @param tagFactory the {@link TagProcessorFactory} that should be used
  * @return this <code>HtmlPipelineContext</code>
  */
 public HtmlPipelineContext SetTagFactory(ITagProcessorFactory tagFactory)
 {
     this.tagFactory = tagFactory;
     return(this);
 }
 public EpcTagsController(ITagProcessorFactory tagProcessorFactory)
 {
     _tagProcessorFactory = tagProcessorFactory;
     _tagProcessor        = _tagProcessorFactory.GetSingleTagProcessor(TagType.SGTIN_96);
 }
 virtual public void SetUp()
 {
     LoggerFactory.GetInstance().SetLogger(new SysoLogger(3));
     tp  = Tags.GetHtmlTagProcessorFactory();
     tpi = new TagProcessorImplementation();
 }
 /**
  * Set the {@link TagProcessorFactory} to be used. For HTML use {@link Tags#getHtmlTagProcessorFactory()}
  * @param tagFactory the {@link TagProcessorFactory} that should be used
  * @return this <code>HtmlPipelineContext</code>
  */
 virtual public HtmlPipelineContext SetTagFactory(ITagProcessorFactory tagFactory) {
     this.tagFactory = tagFactory;
     return this;
 }
Exemple #9
0
 /**
  * Set the {@link TagProcessorFactory} to be used. For HTML use {@link Tags#getHtmlTagProcessorFactory()}
  * @param tagFactory the {@link TagProcessorFactory} that should be used
  * @return this <code>HtmlPipelineContext</code>
  */
 public SvgPipelineContext SetTagFactory(ITagProcessorFactory tagFactory)
 {
     this.tagFactory = tagFactory;
     return this;
 }
Exemple #10
0
 public void SetUp() {
     LoggerFactory.GetInstance().SetLogger(new SysoLogger(3));
     tp = Tags.GetHtmlTagProcessorFactory();
     tpi = new TagProcessorImplementation();
 }
Exemple #11
0
 protected ITagProcessorFactory GetDefaultTagProcessorFactory()
 {
     if (null == tpf) {
         tpf = SvgTags.GetSvgTagProcessorFactory();
     }
     return tpf;
 }