Esempio n. 1
0
 /// <summary>Adds @font-face fonts to the FontProvider.</summary>
 /// <param name="cssResolver">the css styles resolver</param>
 public virtual void AddFontFaceFonts(ICssResolver cssResolver)
 {
     if (cssResolver is SvgStyleResolver)
     {
         foreach (CssFontFaceRule fontFace in ((SvgStyleResolver)cssResolver).GetFonts())
         {
             bool     findSupportedSrc = false;
             FontFace ff = FontFace.Create(fontFace.GetProperties());
             if (ff != null)
             {
                 foreach (FontFace.FontFaceSrc src in ff.GetSources())
                 {
                     if (CreateFont(ff.GetFontFamily(), src))
                     {
                         findSupportedSrc = true;
                         break;
                     }
                 }
             }
             if (!findSupportedSrc)
             {
                 LogManager.GetLogger(typeof(iText.Svg.Processors.Impl.Font.SvgFontProcessor)).Error(MessageFormatUtil.Format
                                                                                                         (iText.StyledXmlParser.LogMessageConstant.UNABLE_TO_RETRIEVE_FONT, fontFace));
             }
         }
     }
 }
Esempio n. 2
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);
        }
Esempio n. 3
0
        /* (non-Javadoc)
         * @see com.itextpdf.html2pdf.attach.IHtmlProcessor#processDocument(com.itextpdf.html2pdf.html.node.INode, com.itextpdf.kernel.pdf.PdfDocument)
         */
        public virtual Document ProcessDocument(INode root, PdfDocument pdfDocument)
        {
            ReflectionUtils.ScheduledLicenseCheck();
            context.Reset(pdfDocument);
            if (!context.HasFonts())
            {
                throw new Html2PdfException(Html2PdfException.FontProviderContainsZeroFonts);
            }
            roots       = new List <IPropertyContainer>();
            cssResolver = new DefaultCssResolver(root, context);
            context.GetLinkContext().ScanForIds(root);
            AddFontFaceFonts();
            root = FindHtmlNode(root);
            Visit(root);
            Document doc = (Document)roots[0];

            // TODO DEVSIX-4261 more precise check if a counter was actually added to the document
            if (context.GetCssContext().IsPagesCounterPresent() && doc.GetRenderer() is HtmlDocumentRenderer)
            {
                doc.Relayout();
            }
            cssResolver = null;
            roots       = null;
            EventCounterHandler.GetInstance().OnEvent(PdfHtmlEvent.CONVERT, context.GetEventCountingMetaInfo(), GetType
                                                          ());
            return(doc);
        }
Esempio n. 4
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;
        }
        public virtual Document ProcessDocument(INode root, PdfDocument pdfDocument)
        {
            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(pdfDocument);
            if (!context.HasFonts())
            {
                throw new Html2PdfException(Html2PdfException.FontProviderContainsZeroFonts);
            }
            // TODO store html version from document type in context if necessary
            roots       = new List <IPropertyContainer>();
            cssResolver = new DefaultCssResolver(root, context);
            context.GetLinkContext().ScanForIds(root);
            AddFontFaceFonts();
            root = FindHtmlNode(root);
            Visit(root);
            Document doc = (Document)roots[0];

            // TODO more precise check if a counter was actually added to the document
            if (context.GetCssContext().IsPagesCounterPresent() && doc.GetRenderer() is HtmlDocumentRenderer)
            {
                doc.Relayout();
            }
            cssResolver = null;
            roots       = null;
            EventCounterHandler.GetInstance().OnEvent(PdfHtmlEvent.CONVERT, context.GetEventCountingMetaInfo(), GetType());
            return(doc);
        }
 /// <summary>Load in configuration, set initial processorState and create/fill-in context of the processor</summary>
 /// <param name="converterProps">that contains configuration properties and operations</param>
 internal virtual void PerformSetup(INode root, ISvgConverterProperties converterProps)
 {
     processorState = new ProcessorState();
     if (converterProps.GetRendererFactory() != null) {
         rendererFactory = converterProps.GetRendererFactory();
     }
     context = new SvgProcessorContext(converterProps);
     cssResolver = new SvgStyleResolver(root, context);
     new SvgFontProcessor(context).AddFontFaceFonts(cssResolver);
     //TODO RND-1042
     namedObjects = new Dictionary<String, ISvgNodeRenderer>();
     cssContext = new SvgCssContext();
 }
        /* (non-Javadoc)
         * @see com.itextpdf.html2pdf.attach.IHtmlProcessor#processDocument(com.itextpdf.html2pdf.html.node.INode, com.itextpdf.kernel.pdf.PdfDocument)
         */
        public virtual Document ProcessDocument(INode root, PdfDocument pdfDocument)
        {
            ReflectionUtils.ScheduledLicenseCheck();
            context.Reset(pdfDocument);
            if (!context.HasFonts())
            {
                throw new Html2PdfException(Html2PdfException.FontProviderContainsZeroFonts);
            }
            roots       = new List <IPropertyContainer>();
            cssResolver = new DefaultCssResolver(root, context);
            context.GetLinkContext().ScanForIds(root);
            AddFontFaceFonts();
            root = FindHtmlNode(root);
            if (context.GetCssContext().IsNonPagesTargetCounterPresent())
            {
                VisitToProcessCounters(root);
                context.GetCssContext().GetCounterManager().ClearManager();
            }
            Visit(root);
            HtmlDocument doc = (HtmlDocument)roots[0];

            // TODO DEVSIX-4261 more precise check if a counter was actually added to the document
            if (context.GetCssContext().IsPagesCounterPresent())
            {
                if (doc.GetRenderer() is HtmlDocumentRenderer)
                {
                    ((HtmlDocumentRenderer)doc.GetRenderer()).ProcessWaitingElement();
                    int counter = 0;
                    do
                    {
                        ++counter;
                        doc.Relayout();
                        if (counter >= context.GetLimitOfLayouts())
                        {
                            logger.Warn(MessageFormatUtil.Format(iText.Html2pdf.LogMessageConstant.EXCEEDED_THE_MAXIMUM_NUMBER_OF_RELAYOUTS
                                                                 ));
                            break;
                        }
                    }while (((DocumentRenderer)doc.GetRenderer()).IsRelayoutRequired());
                }
                else
                {
                    logger.Warn(iText.Html2pdf.LogMessageConstant.CUSTOM_RENDERER_IS_SET_FOR_HTML_DOCUMENT);
                }
            }
            cssResolver = null;
            roots       = null;
            EventCounterHandler.GetInstance().OnEvent(PdfHtmlEvent.CONVERT, context.GetEventCountingMetaInfo(), GetType
                                                          ());
            return(doc);
        }
Esempio n. 8
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);
        }
Esempio n. 9
0
        public CssWriter(TextWriter writer, CssContext context = null, CssScope scope = null, ICssResolver resolver = null)
        {
            #region Preconditions

            if (writer == null) throw new ArgumentNullException(nameof(writer));

            #endregion

            this.writer = writer;
            this.context = context ?? new CssContext();
            this.resolver = resolver;
            this.scope = scope ?? new CssScope();

            this.browserSupport = this.context.BrowserSupport;
        }
Esempio n. 10
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);
     }
 }
Esempio n. 11
0
 /// <summary>Processes the page rules.</summary>
 /// <param name="rootNode">the root node</param>
 /// <param name="cssResolver">the CSS resolver</param>
 /// <param name="context">the processor context</param>
 public virtual void ProcessPageRules(INode rootNode, ICssResolver cssResolver, ProcessorContext context) {
     PageContextProperties firstPageProps = PageContextProperties.Resolve(rootNode, cssResolver, context.GetCssContext
         (), PageContextConstants.FIRST, PageContextConstants.RIGHT);
     // TODO in documents with set to rtl on root document, first page is considered as left
     PageContextProperties leftPageProps = PageContextProperties.Resolve(rootNode, cssResolver, context.GetCssContext
         (), PageContextConstants.LEFT);
     PageContextProperties rightPageProps = PageContextProperties.Resolve(rootNode, cssResolver, context.GetCssContext
         (), PageContextConstants.RIGHT);
     PageSize defaultPageSize = document.GetPdfDocument().GetDefaultPageSize();
     float[] defaultPageMargins = new float[] { document.GetTopMargin(), document.GetRightMargin(), document.GetBottomMargin
         (), document.GetRightMargin() };
     firstPageProc = new PageContextProcessor(firstPageProps, context, defaultPageSize, defaultPageMargins);
     leftPageProc = new PageContextProcessor(leftPageProps, context, defaultPageSize, defaultPageMargins);
     rightPageProc = new PageContextProcessor(rightPageProps, context, defaultPageSize, defaultPageMargins);
     handler = new HtmlDocumentRenderer.PageMarginBoxesDrawingHandler().SetHtmlDocumentRenderer(this);
     document.GetPdfDocument().AddEventHandler(PdfDocumentEvent.END_PAGE, handler);
 }
Esempio n. 12
0
 /// <summary>Processes the page rules.</summary>
 /// <param name="rootNode">the root node</param>
 /// <param name="cssResolver">the css resolver</param>
 /// <param name="context">the context</param>
 public virtual void ProcessPageRules(INode rootNode, ICssResolver cssResolver, ProcessorContext context)
 {
     ((HtmlDocumentRenderer)document.GetRenderer()).ProcessPageRules(rootNode, cssResolver, context);
 }
Esempio n. 13
0
 public void SetResolver(ICssResolver resolver)
 {
     this.resolver = resolver;
 }
Esempio n. 14
0
        /// <summary>
        /// Resolves a node with a
        /// <see cref="PageContextProperties"/>
        /// instance as result.
        /// </summary>
        /// <param name="rootNode">the root node to resolve</param>
        /// <param name="cssResolver">the CSS resolver</param>
        /// <param name="context">the CSS context</param>
        /// <param name="pageClasses">the page classes</param>
        /// <returns>
        /// the
        /// <see cref="PageContextProperties"/>
        /// for a specific node
        /// </returns>
        public static iText.Html2pdf.Attach.Impl.Layout.PageContextProperties Resolve(INode rootNode, ICssResolver
                                                                                      cssResolver, CssContext context, params String[] pageClasses)
        {
            PageContextNode pageProps = GetResolvedPageClassNode(rootNode, cssResolver, context, pageClasses);
            IList <PageMarginBoxContextNode> pagesMarginBoxes = GetResolvedMarginBoxes(pageProps, cssResolver, context);

            return(new iText.Html2pdf.Attach.Impl.Layout.PageContextProperties(pageProps, pagesMarginBoxes));
        }
Esempio n. 15
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);
        }
Esempio n. 16
0
 public void SetResolver(ICssResolver resolver)
 {
     this.resolver = resolver;
 }