Beispiel #1
0
        /* (non-Javadoc)
         * @see com.itextpdf.html2pdf.attach.IHtmlProcessor#processElements(com.itextpdf.html2pdf.html.node.INode)
         */
        public virtual IList <IElement> ProcessElements(INode root)
        {
            ReflectionUtils.ScheduledLicenseCheck();
            context.Reset();
            roots       = new List <IPropertyContainer>();
            cssResolver = new DefaultCssResolver(root, context);
            context.GetLinkContext().ScanForIds(root);
            AddFontFaceFonts();
            IElementNode html = FindHtmlNode(root);
            IElementNode body = FindBodyNode(root);

            // Force resolve styles to fetch default font size etc
            html.SetStyles(cssResolver.ResolveStyles(html, context.GetCssContext()));
            // visit body
            Visit(body);
            Div bodyDiv = (Div)roots[0];
            IList <IElement> elements = new List <IElement>();

            // re-resolve body element styles in order to use them in top-level elements properties setting
            body.SetStyles(cssResolver.ResolveStyles(body, context.GetCssContext()));
            foreach (IPropertyContainer propertyContainer in bodyDiv.GetChildren())
            {
                if (propertyContainer is IElement)
                {
                    SetConvertedRootElementProperties(body.GetStyles(), context, propertyContainer);
                    elements.Add((IElement)propertyContainer);
                }
            }
            cssResolver = null;
            roots       = null;
            EventCounterHandler.GetInstance().OnEvent(PdfHtmlEvent.CONVERT, context.GetEventCountingMetaInfo(), GetType
                                                          ());
            return(elements);
        }
Beispiel #2
0
        public virtual IList<IElement> ProcessElements(INode root) {

            try 
            {
                String licenseKeyClassName = "iText.License.LicenseKey, itext.licensekey";
                String licenseKeyProductClassName = "iText.License.LicenseKeyProduct, itext.licensekey";
                String licenseKeyFeatureClassName = "iText.License.LicenseKeyProductFeature, itext.licensekey";
                String checkLicenseKeyMethodName = "ScheduledCheck";
                Type licenseKeyClass = GetClass(licenseKeyClassName);
                if ( licenseKeyClass != null ) 
                {                
                    Type licenseKeyProductClass = GetClass(licenseKeyProductClassName);
                    Type licenseKeyProductFeatureClass = GetClass(licenseKeyFeatureClassName);
                    Array array = Array.CreateInstance(licenseKeyProductFeatureClass, 0);
                    object[] objects = new object[]
                    {
                        Html2PdfProductInfo.PRODUCT_NAME,
                        Html2PdfProductInfo.MAJOR_VERSION,
                        Html2PdfProductInfo.MINOR_VERSION,
                        array
                    };
                    Object productObject = System.Activator.CreateInstance(licenseKeyProductClass, objects);
                    MethodInfo m = licenseKeyClass.GetMethod(checkLicenseKeyMethodName);
                    m.Invoke(System.Activator.CreateInstance(licenseKeyClass), new object[] {productObject});
                }   
            } 
            catch ( Exception e ) 
            {
                if ( !Kernel.Version.IsAGPLVersion() )
                {
                    throw;
                }
            }
            context.Reset();
            roots = new List<IPropertyContainer>();
            cssResolver = new DefaultCssResolver(root, context);
            context.GetLinkContext().ScanForIds(root);
            AddFontFaceFonts();
            IElementNode html = FindHtmlNode(root);
            IElementNode body = FindBodyNode(root);
            // Force resolve styles to fetch default font size etc
            html.SetStyles(cssResolver.ResolveStyles(html, context.GetCssContext()));
            // visit body
            Visit(body);
            Div bodyDiv = (Div)roots[0];
            IList<IElement> elements = new List<IElement>();
            // re-resolve body element styles in order to use them in top-level elements properties setting
            body.SetStyles(cssResolver.ResolveStyles(body, context.GetCssContext()));
            foreach (IPropertyContainer propertyContainer in bodyDiv.GetChildren()) {
                if (propertyContainer is IElement) {
                    SetConvertedRootElementProperties(body.GetStyles(), context, propertyContainer);
                    elements.Add((IElement)propertyContainer);
                }
            }
            cssResolver = null;
            roots = null;
            EventCounterHandler.GetInstance().OnEvent(PdfHtmlEvent.CONVERT, context.GetEventCountingMetaInfo(), GetType());
            return elements;
        }
 /// <summary>Start the depth-first traversal of the INode tree, pushing the results on the stack</summary>
 /// <param name="startingNode">node to start on</param>
 internal virtual void ExecuteDepthFirstTraversal(INode startingNode)
 {
     //Create and push rootNode
     if (startingNode is IElementNode && !rendererFactory.IsTagIgnored((IElementNode)startingNode)) {
         IElementNode rootElementNode = (IElementNode)startingNode;
         ISvgNodeRenderer startingRenderer = rendererFactory.CreateSvgNodeRendererForTag(rootElementNode, null);
         if (startingRenderer != null) {
             IDictionary<String, String> attributesAndStyles = cssResolver.ResolveStyles(startingNode, cssContext);
             rootElementNode.SetStyles(attributesAndStyles);
             startingRenderer.SetAttributesAndStyles(attributesAndStyles);
             processorState.Push(startingRenderer);
             foreach (INode rootChild in startingNode.ChildNodes()) {
                 Visit(rootChild);
             }
         }
     }
 }
Beispiel #4
0
        /// <summary>Gets the resolved page class node.</summary>
        /// <param name="rootNode">the root node</param>
        /// <param name="cssResolver">the CSS resolver</param>
        /// <param name="context">the CSS context</param>
        /// <param name="pageClasses">the page classes</param>
        /// <returns>the resolved page class node</returns>
        private static PageContextNode GetResolvedPageClassNode(INode rootNode, ICssResolver cssResolver, CssContext
                                                                context, params String[] pageClasses)
        {
            PageContextNode pagesClassNode = new PageContextNode(rootNode);

            foreach (String pageClass in pageClasses)
            {
                pagesClassNode.AddPageClass(pageClass);
            }
            IDictionary <String, String> pageClassStyles = cssResolver.ResolveStyles(pagesClassNode, context);

            pagesClassNode.SetStyles(pageClassStyles);
            return(pagesClassNode);
        }
Beispiel #5
0
 private void ResolveStylesForTree(INode node, ICssResolver cssResolver, CssContext context)
 {
     if (node is IElementNode)
     {
         IElementNode element = (IElementNode)node;
         element.SetStyles(cssResolver.ResolveStyles((IElementNode)node, context));
         if (TagConstants.HTML.Equals(element.Name()))
         {
             context.SetRootFontSize(element.GetStyles().Get(CssConstants.FONT_SIZE));
         }
     }
     foreach (INode child in node.ChildNodes())
     {
         ResolveStylesForTree(child, cssResolver, context);
     }
 }
Beispiel #6
0
        /// <summary>Gets the resolved margin boxes.</summary>
        /// <param name="pageClassNode">the page contex node</param>
        /// <param name="cssResolver">the CSS resolver</param>
        /// <param name="context">the CSS context</param>
        /// <returns>the resolved margin boxes</returns>
        private static IList <PageMarginBoxContextNode> GetResolvedMarginBoxes(PageContextNode pageClassNode, ICssResolver
                                                                               cssResolver, CssContext context)
        {
            IList <PageMarginBoxContextNode> resolvedMarginBoxes = new List <PageMarginBoxContextNode>();

            foreach (String pageMarginBoxName in pageMarginBoxNames)
            {
                PageMarginBoxContextNode     marginBoxNode   = new PageMarginBoxContextNode(pageClassNode, pageMarginBoxName);
                IDictionary <String, String> marginBoxStyles = cssResolver.ResolveStyles(marginBoxNode, context);
                if (!marginBoxNode.ChildNodes().IsEmpty())
                {
                    marginBoxNode.SetStyles(marginBoxStyles);
                    resolvedMarginBoxes.Add(marginBoxNode);
                }
                context.SetQuotesDepth(0);
            }
            return(resolvedMarginBoxes);
        }
Beispiel #7
0
 /// <summary>Recursively processes a node converting HTML into PDF using tag workers.</summary>
 /// <param name="node">the node</param>
 private void Visit(INode node)
 {
     if (node is IElementNode)
     {
         IElementNode element = (IElementNode)node;
         element.SetStyles(cssResolver.ResolveStyles(element, context.GetCssContext()));
         if (!IsDisplayable(element))
         {
             return;
         }
         ITagWorker tagWorker = context.GetTagWorkerFactory().GetTagWorker(element, context);
         if (tagWorker == null)
         {
             if (!ignoredTags.Contains(element.Name()))
             {
                 logger.Error(MessageFormatUtil.Format(iText.Html2pdf.LogMessageConstant.NO_WORKER_FOUND_FOR_TAG, (element)
                                                       .Name()));
             }
         }
         else
         {
             context.GetState().Push(tagWorker);
         }
         if (tagWorker is HtmlTagWorker)
         {
             ((HtmlTagWorker)tagWorker).ProcessPageRules(node, cssResolver, context);
         }
         if (TagConstants.BODY.Equals(element.Name()) || TagConstants.HTML.Equals(element.Name()))
         {
             RunApplier(element, tagWorker);
         }
         context.GetOutlineHandler().AddOutlineAndDestToDocument(tagWorker, element, context);
         VisitPseudoElement(element, tagWorker, CssConstants.BEFORE);
         VisitPseudoElement(element, tagWorker, CssConstants.PLACEHOLDER);
         foreach (INode childNode in element.ChildNodes())
         {
             if (!context.IsProcessingInlineSvg())
             {
                 Visit(childNode);
             }
         }
         VisitPseudoElement(element, tagWorker, CssConstants.AFTER);
         if (tagWorker != null)
         {
             tagWorker.ProcessEnd(element, context);
             LinkHelper.CreateDestination(tagWorker, element, context);
             context.GetOutlineHandler().SetDestinationToElement(tagWorker, element);
             context.GetState().Pop();
             if (!TagConstants.BODY.Equals(element.Name()) && !TagConstants.HTML.Equals(element.Name()))
             {
                 RunApplier(element, tagWorker);
             }
             if (!context.GetState().Empty())
             {
                 PageBreakApplierUtil.AddPageBreakElementBefore(context, context.GetState().Top(), element, tagWorker);
                 tagWorker = ProcessRunningElement(tagWorker, element, context);
                 bool childProcessed = context.GetState().Top().ProcessTagChild(tagWorker, context);
                 PageBreakApplierUtil.AddPageBreakElementAfter(context, context.GetState().Top(), element, tagWorker);
                 if (!childProcessed && !ignoredChildTags.Contains(element.Name()))
                 {
                     logger.Error(MessageFormatUtil.Format(iText.Html2pdf.LogMessageConstant.WORKER_UNABLE_TO_PROCESS_OTHER_WORKER
                                                           , context.GetState().Top().GetType().FullName, tagWorker.GetType().FullName));
                 }
             }
             else
             {
                 if (tagWorker.GetElementResult() != null)
                 {
                     roots.Add(tagWorker.GetElementResult());
                 }
             }
         }
         element.SetStyles(null);
     }
     else
     {
         if (node is ITextNode)
         {
             String content = ((ITextNode)node).WholeText();
             if (content != null)
             {
                 if (!context.GetState().Empty())
                 {
                     bool contentProcessed = context.GetState().Top().ProcessContent(content, context);
                     if (!contentProcessed)
                     {
                         logger.Error(MessageFormatUtil.Format(iText.Html2pdf.LogMessageConstant.WORKER_UNABLE_TO_PROCESS_IT_S_TEXT_CONTENT
                                                               , context.GetState().Top().GetType().FullName));
                     }
                 }
                 else
                 {
                     logger.Error(iText.Html2pdf.LogMessageConstant.NO_CONSUMER_FOUND_FOR_CONTENT);
                 }
             }
         }
     }
 }