SetAcceptUnknown() public method

public SetAcceptUnknown ( bool acceptUnknown ) : HtmlPipelineContext
acceptUnknown bool
return HtmlPipelineContext
Ejemplo n.º 1
1
        public byte[] GeneratePdfFromView(string viewString, string[] cssPaths, string fontPath)
        {
            using (var memoryStream = new MemoryStream())
            {
                var doc = new Document(PageSize.A4);
                var writer = PdfWriter.GetInstance(doc, memoryStream);

                doc.Open();

                var tagProcessors = InitializeTagProcessor();
                var cssResolver = InitializeCssFiles(cssPaths);

                var fontProvider = new CustomFontFactory(fontPath);

                var hpc = new HtmlPipelineContext(new CssAppliersImpl(fontProvider));
                hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(tagProcessors);

                var htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(doc, writer));
                var pipeline = new CssResolverPipeline(cssResolver, htmlPipeline);

                var worker = new XMLWorker(pipeline, true);
                var xmlParser = new XMLParser(true, worker, Encoding.Unicode);
                xmlParser.Parse(new StringReader(viewString));

                doc.Close();
                return memoryStream.ToArray();
            }
        }
        protected override void MakePdf(string outPdf) {
            Document doc = new Document(PageSize.A4.Rotate());
            PdfWriter pdfWriter = PdfWriter.GetInstance(doc, new FileStream(outPdf, FileMode.Create));
            doc.SetMargins(45, 45, 0, 100);
            doc.Open();


            CssFilesImpl cssFiles = new CssFilesImpl();
            cssFiles.Add(
                XMLWorkerHelper.GetCSS(
                    File.OpenRead(RESOURCES + Path.DirectorySeparatorChar + testPath + Path.DirectorySeparatorChar + testName +
                                  Path.DirectorySeparatorChar + "complexDiv_files" + Path.DirectorySeparatorChar +
                                  "main.css")));
            cssFiles.Add(
                XMLWorkerHelper.GetCSS(
                    File.OpenRead(RESOURCES + Path.DirectorySeparatorChar + testPath + Path.DirectorySeparatorChar + testName +
                                  Path.DirectorySeparatorChar + "complexDiv_files" + Path.DirectorySeparatorChar +
                                  "widget082.css")));
            StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
            HtmlPipelineContext hpc =
                new HtmlPipelineContext(
                    new CssAppliersImpl(new XMLWorkerFontProvider(RESOURCES + @"\tool\xml\examples\fonts")));
            hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(Tags.GetHtmlTagProcessorFactory());
            hpc.SetImageProvider(new SampleTestImageProvider());
            hpc.SetPageSize(doc.PageSize);
            HtmlPipeline htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(doc, pdfWriter));
            IPipeline pipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
            XMLWorker worker = new XMLWorker(pipeline, true);
            XMLParser p = new XMLParser(true, worker, Encoding.GetEncoding("UTF-8"));
            p.Parse(File.OpenRead(inputHtml), Encoding.GetEncoding("UTF-8"));
            doc.Close();
        }
Ejemplo n.º 3
0
        protected override void TransformHtml2Pdf() {
            Document doc = new Document(PageSize.A1.Rotate());
            PdfWriter pdfWriter = PdfWriter.GetInstance(doc, new FileStream(outPdf, FileMode.Create));
            doc.SetMargins(doc.LeftMargin - 10, doc.RightMargin - 10, doc.TopMargin, doc.BottomMargin);
            doc.Open();


            CssFilesImpl cssFiles = new CssFilesImpl();
            cssFiles.Add(
                XMLWorkerHelper.GetCSS(
                    File.OpenRead(RESOURCES + Path.DirectorySeparatorChar + testPath + testName +
                                  Path.DirectorySeparatorChar + "complexDiv02_files" + Path.DirectorySeparatorChar +
                                  "minimum0.css")));
            cssFiles.Add(
                XMLWorkerHelper.GetCSS(
                    File.OpenRead(RESOURCES + Path.DirectorySeparatorChar + testPath + testName +
                                  Path.DirectorySeparatorChar + "complexDiv02_files" + Path.DirectorySeparatorChar +
                                  "print000.css")));
            cssFiles.Add(XMLWorkerHelper.GetCSS(File.OpenRead(RESOURCES + @"\tool\xml\examples\" + "sampleTest.css")));
            StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
            HtmlPipelineContext hpc =
                new HtmlPipelineContext(
                    new CssAppliersImpl(new XMLWorkerFontProvider(RESOURCES + @"\tool\xml\examples\fonts")));
            hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(Tags.GetHtmlTagProcessorFactory());
            hpc.SetImageProvider(new SampleTestImageProvider());
            HtmlPipeline htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(doc, pdfWriter));
            IPipeline pipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
            XMLWorker worker = new XMLWorker(pipeline, true);
            XMLParser p = new XMLParser(true, worker, Encoding.GetEncoding("UTF-8"));
            p.Parse(File.OpenRead(inputHtml), Encoding.GetEncoding("UTF-8"));
            doc.Close();
        }
Ejemplo n.º 4
0
 virtual public void ParseXfaOnlyXML() {
     StreamReader bis = File.OpenText(RESOURCE_TEST_PATH + SNIPPETS + TEST + "snippet.html");
     Document doc = new Document(PageSize.A4);
     float margin = utils.ParseRelativeValue("10%", PageSize.A4.Width);
     doc.SetMargins(margin, margin, margin, margin);
     PdfWriter writer = null;
     try {
         writer = PdfWriter.GetInstance(doc, new FileStream(
             TARGET + TEST + "_charset.pdf", FileMode.Create));
     }
     catch (DocumentException e) {
         Console.WriteLine(e);
     }
     CssFilesImpl cssFiles = new CssFilesImpl();
     cssFiles.Add(XMLWorkerHelper.GetInstance().GetDefaultCSS());
     StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
     HtmlPipelineContext hpc = new HtmlPipelineContext(null);
     hpc.SetAcceptUnknown(true)
         .AutoBookmark(true)
         .SetTagFactory(Tags.GetHtmlTagProcessorFactory())
         .CharSet(Encoding.GetEncoding("ISO-8859-1"));
     IPipeline pipeline = new CssResolverPipeline(cssResolver,
         new HtmlPipeline(hpc, new PdfWriterPipeline(doc, writer)));
     XMLWorker worker = new XMLWorker(pipeline, true);
     doc.Open();
     XMLParser p = new XMLParser(true, worker);
     p.Parse(bis);
     doc.Close();
 }
Ejemplo n.º 5
0
 public void SetUp() {
     LoggerFactory.GetInstance().SetLogger(new SysoLogger(3));
     cssFiles = new CssFilesImpl();
     String path = RESOURCES + @"\css\test.css";
     path = path.Substring(0, path.LastIndexOf("test.css"));
     FileRetrieveImpl r = new FileRetrieveImpl(new String[] {path});
     StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles, r);
     HtmlPipelineContext hpc = new HtmlPipelineContext(null);
     hpc.SetAcceptUnknown(false).AutoBookmark(true).SetTagFactory(Tags.GetHtmlTagProcessorFactory());
     IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(hpc, null));
     XMLWorker worker = new XMLWorker(pipeline, true);
     p = new XMLParser(worker);
 }
Ejemplo n.º 6
0
        protected override void MakePdf(string outPdf) {
            Document doc = new Document(PageSize.A3.Rotate());

            PdfWriter pdfWriter = PdfWriter.GetInstance(doc, new FileStream(outPdf, FileMode.Create));
            pdfWriter.CreateXmpMetadata();

            doc.SetMargins(200, 200, 0, 0);
            doc.Open();


            CssFilesImpl cssFiles = new CssFilesImpl();
            cssFiles.Add(
                XMLWorkerHelper.GetCSS(
                    File.OpenRead(RESOURCES + Path.DirectorySeparatorChar + testPath + Path.DirectorySeparatorChar + testName +
                                  Path.DirectorySeparatorChar + "complexDiv_files" + Path.DirectorySeparatorChar +
                                  "main.css")));
            cssFiles.Add(
                XMLWorkerHelper.GetCSS(
                    File.OpenRead(RESOURCES + Path.DirectorySeparatorChar + testPath + Path.DirectorySeparatorChar + testName +
                                  Path.DirectorySeparatorChar + "complexDiv_files" + Path.DirectorySeparatorChar +
                                  "widget082.css")));
            StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
            HtmlPipelineContext hpc =
                new HtmlPipelineContext(
                    new CssAppliersImpl(new XMLWorkerFontProvider(RESOURCES + @"\tool\xml\examples\fonts")));
            hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(Tags.GetHtmlTagProcessorFactory());
            hpc.SetImageProvider(new SampleTestImageProvider());
            hpc.SetPageSize(doc.PageSize);
            HtmlPipeline htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(doc, pdfWriter));
            IPipeline pipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
            XMLWorker worker = new XMLWorker(pipeline, true);
            XMLParser p = new XMLParser(true, worker, Encoding.GetEncoding("UTF-8"));
            p.Parse(File.OpenRead(inputHtml), Encoding.GetEncoding("UTF-8"));
            //ICC_Profile icc = ICC_Profile.getInstance(ComplexDiv01Test.class.getResourceAsStream("sRGB Color Space Profile.icm"));
            //pdfWriter.setOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
            doc.Close();
        }
Ejemplo n.º 7
0
 /**
  * @param d the ElementHandler
  * @param inp the Stream
  * @throws IOException if something went seriously wrong with IO.
  */
 public void ParseXHtml(IElementHandler d, Stream inp, Encoding charset) {
     CssFilesImpl cssFiles = new CssFilesImpl();
     cssFiles.Add(GetDefaultCSS());
     StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
     HtmlPipelineContext hpc = new HtmlPipelineContext(null);
     hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(GetDefaultTagProcessorFactory());
     IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(hpc, new ElementHandlerPipeline(d,
             null)));
     XMLWorker worker = new XMLWorker(pipeline, true);
     XMLParser p = new XMLParser(true, worker, charset);
     if (charset != null) {
         p.Parse(inp, charset);
     } else {
         p.Parse(inp);
     }
 }
Ejemplo n.º 8
0
 public void ParseXHtml(PdfWriter writer, Document doc, Stream inp, Stream inCssFile, Encoding charset, IFontProvider fontProvider) {
     CssFilesImpl cssFiles = new CssFilesImpl();
     if (inCssFile != null)
         cssFiles.Add(GetCSS(inCssFile));
     else
         cssFiles.Add(GetDefaultCSS());
     StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
     HtmlPipelineContext hpc = new HtmlPipelineContext(new CssAppliersImpl(fontProvider));
     hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(GetDefaultTagProcessorFactory());
     HtmlPipeline htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(doc, writer));
     IPipeline pipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
     XMLWorker worker = new XMLWorker(pipeline, true);
     XMLParser p = new XMLParser(true, worker, charset);
     if (charset != null) {
         p.Parse(inp, charset);
     } else {
         p.Parse(inp);
     }
 }
Ejemplo n.º 9
0
 /**
  * Parses the xml data. This method configures the XMLWorker to parse
  * (X)HTML/CSS and accept unknown tags. Writes the output in the given
  * PdfWriter with the given document.
  *
  * @param writer the PdfWriter
  * @param doc the Document
  * @param inp the reader
  * @throws IOException thrown when something went wrong with the IO
  */
 public void ParseXHtml(PdfWriter writer, Document doc, TextReader inp) {
     CssFilesImpl cssFiles = new CssFilesImpl();
     cssFiles.Add(GetDefaultCSS());
     StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
     HtmlPipelineContext hpc = new HtmlPipelineContext(null);
     hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(GetDefaultTagProcessorFactory());
     IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(hpc, new PdfWriterPipeline(doc,
             writer)));
     XMLWorker worker = new XMLWorker(pipeline, true);
     XMLParser p = new XMLParser();
     p.AddListener(worker);
     p.Parse(inp);
 }
Ejemplo n.º 10
0
        static void Main(string[] args) {
            if (args.Length < 2) {
                Console.WriteLine("Invalid number of arguments.");
                Console.WriteLine("Usage: html2Pdf.exe [input html_file/directory] [default css file]");
                return;
            }

            List<FileStream> fileList = new List<FileStream>();
            if (File.Exists(args[0])) {
                fileList.Add(new FileStream(args[0], FileMode.Open));
            } else if (Directory.Exists(args[0])) {
                CollectHtmlFiles(fileList, args[0]);
            }

            if (fileList.Count == 0) {
                Console.WriteLine("Invalid html_file/directory");
                Console.WriteLine("Usage: html2Pdf.exe [input html_file/directory] [default css file]");
                return;    
            }

            foreach (FileStream fileStream in fileList)
            {
                Document doc = new Document(PageSize.LETTER);
                doc.SetMargins(doc.LeftMargin, doc.RightMargin, 35, 0);
                String path = Path.GetDirectoryName(Path.GetFullPath(fileStream.Name)) + Path.DirectorySeparatorChar +
                              Path.GetFileNameWithoutExtension(fileStream.Name) + ".pdf";
                PdfWriter pdfWriter = PdfWriter.GetInstance(doc, new FileStream(path, FileMode.Create));

                doc.Open();
                Dictionary<String, String> substFonts = new Dictionary<String, String>();
                substFonts["Arial Unicode MS"] = "Helvetica";
                CssFilesImpl cssFiles = new CssFilesImpl();
                cssFiles.Add(XMLWorkerHelper.GetCSS(new FileStream(args[1], FileMode.Open)));
                StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
                HtmlPipelineContext hpc = new HtmlPipelineContext(new CssAppliersImpl(new UnembedFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS, substFonts)));
                hpc.SetImageProvider(new ImageProvider(args[0]));
                hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(Tags.GetHtmlTagProcessorFactory());
                HtmlPipeline htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(doc, pdfWriter));
                IPipeline pipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
                XMLWorker worker = new XMLWorker(pipeline, true);
                XMLParser xmlParse = new XMLParser(true, worker, null);
		        xmlParse.Parse(fileStream);
                doc.Close();

                String cmpPath = Path.GetDirectoryName(Path.GetFullPath(fileStream.Name)) + Path.DirectorySeparatorChar +
                                 "cmp_" + Path.GetFileNameWithoutExtension(fileStream.Name) + ".pdf";
                if (File.Exists(cmpPath)) {
                    CompareTool ct = new CompareTool(path, cmpPath);
                    String outImage = "<testName>-%03d.png".Replace("<testName>", Path.GetFileNameWithoutExtension(fileStream.Name) );
                    String cmpImage = "cmp_<testName>-%03d.png".Replace("<testName>", Path.GetFileNameWithoutExtension(fileStream.Name) );
                    String diffPath = Path.GetDirectoryName(Path.GetFullPath(fileStream.Name)) +
                                      Path.DirectorySeparatorChar + "diff_" + Path.GetFileNameWithoutExtension(fileStream.Name);
                    String errorMessage = ct.Compare(Path.GetDirectoryName(Path.GetFullPath(fileStream.Name)) + Path.DirectorySeparatorChar + "compare" + Path.DirectorySeparatorChar, diffPath);
                    if (errorMessage != null) {
                        Console.WriteLine(errorMessage);
                    }
                }
            }
        }
Ejemplo n.º 11
0
        private void processHtml(IElementHandler elementsHandler)
        {
            var cssResolver = new StyleAttrCSSResolver();

            if (CssFilesPath != null && CssFilesPath.Any())
            {
                foreach (var cssFile in CssFilesPath)
                {
                    cssResolver.AddCss(XmlWorkerUtils.GetCssFile(cssFile));
                }
            }

            if (!string.IsNullOrEmpty(InlineCss))
            {
                cssResolver.AddCss(InlineCss, "utf-8", true);
            }

            var htmlContext = new HtmlPipelineContext(new CssAppliersImpl(new UnicodeFontProvider(DefaultFont)));
            if (!string.IsNullOrEmpty(ImagesPath))
            {
                htmlContext.SetImageProvider(new ImageProvider { ImagesPath = ImagesPath });
            }
            htmlContext.CharSet(Encoding.UTF8);

            var tagsProcessorFactory = (DefaultTagProcessorFactory)Tags.GetHtmlTagProcessorFactory();
            if (PdfElement != null)
            {
                tagsProcessorFactory.AddProcessor("totalpagesnumber", new TotalPagesNumberXmlWorkerProcessor(PdfElement));
            }

            htmlContext.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(tagsProcessorFactory);
            var pipeline = new CssResolverPipeline(cssResolver,
                                                   new HtmlPipeline(htmlContext, new ElementHandlerPipeline(elementsHandler, null)));
            var worker = new XMLWorker(pipeline, parseHtml: true);
            var parser = new XMLParser();
            parser.AddListener(worker);
            parser.Parse(new StringReader(Html));
        }