public void ManipulatePdf(string htmlSource, string pdfDest)
        {
            PdfWriter   writer = new PdfWriter(pdfDest);
            PdfDocument pdfDoc = new PdfDocument(writer);

            // Default provider will register standard fonts and free fonts shipped with iText, but not system fonts
            FontProvider provider = new DefaultFontProvider();

            // 1. Register all fonts in a directory
            provider.AddDirectory(FONT_FOLDER);

            // 2. Register a single font by specifying path
            provider.AddFont(FONT1);

            // 3. Use the raw bytes of the font file
            byte[] fontBytes = File.ReadAllBytes(FONT2);
            provider.AddFont(fontBytes);

            // Make sure the provider is used
            ConverterProperties converterProperties = new ConverterProperties()
                                                      // Base URI is required to resolve the path to source files
                                                      .SetBaseUri(SRC)
                                                      .SetFontProvider(provider);

            HtmlConverter.ConvertToPdf(new FileStream(htmlSource, FileMode.Open), pdfDoc, converterProperties);

            pdfDoc.Close();
        }
Beispiel #2
0
        public void ManipulatePdf(string HtmlContent, string basePath)
        {
            string FONTTABLEHEADER  = basePath + @"fonts\Metric-Bold.otf";
            string FONTTABLEGENERAL = basePath + @"fonts\Metric-Regular.otf";
            string FONTPAGEHEADING  = basePath + @"fonts\Indian Type Foundry - Zahrah Bold Italic.otf";
            string HOMEPAGEHEADING  = basePath + @"fonts\Indian Type Foundry - Zahrah Light.otf";

            ConverterProperties properties = new ConverterProperties();

            properties.SetBaseUri(basePath);
            FontProvider fontProvider     = new DefaultFontProvider(false, false, false);
            FontProgram  fontTableHeader  = FontProgramFactory.CreateFont(FONTTABLEHEADER);
            FontProgram  fontTableGeneral = FontProgramFactory.CreateFont(FONTTABLEGENERAL);
            FontProgram  fontPageHeading  = FontProgramFactory.CreateFont(FONTPAGEHEADING);
            FontProgram  homePageHeading  = FontProgramFactory.CreateFont(HOMEPAGEHEADING);

            fontProvider.AddFont(homePageHeading);
            fontProvider.AddFont(fontTableHeader);
            fontProvider.AddFont(fontTableGeneral);
            fontProvider.AddFont(fontPageHeading);
            properties.SetFontProvider(fontProvider);
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(basePath + @"Pdf\Test.pdf"));

            pdfDoc.SetDefaultPageSize(iText.Kernel.Geom.PageSize.LETTER.Rotate());
            HtmlConverter.ConvertToPdf(HtmlContent, pdfDoc, properties);
        }
Beispiel #3
0
        protected internal override ConverterProperties GetConverterProperties()
        {
            DefaultFontProvider fontProvider = new DefaultFontProvider();

            fontProvider.AddFont(AHEM_FONT_PATH);
            return(new ConverterProperties().SetFontProvider(fontProvider));
        }
Beispiel #4
0
        /// <summary>
        /// Creates the PDF file.
        /// </summary>
        /// <param name="src">the path to the source HTML file</param>
        /// <param name="font">the path to an extra font</param>
        /// <param name="dest">the path to the resulting PDF</param>
        public void CreatePdf(String src, String font, String dest)
        {
            ConverterProperties properties   = new ConverterProperties();
            FontProvider        fontProvider = new DefaultFontProvider();
            FontProgram         fontProgram  = FontProgramFactory.CreateFont(font);

            fontProvider.AddFont(fontProgram);
            properties.SetFontProvider(fontProvider);
            HtmlConverter.ConvertToPdf(new FileInfo(src), new FileInfo(dest), properties);
        }
Beispiel #5
0
        public virtual void WordBreakMidNumbersTest()
        {
            FontProvider fontProvider = new DefaultFontProvider();

            fontProvider.AddFont(fontsFolder + "NotoSansCJKjp-Regular.otf");
            ConverterProperties converterProperties = new ConverterProperties();

            converterProperties.SetFontProvider(fontProvider);
            HtmlConverter.ConvertToPdf(new FileInfo(sourceFolder + "wordBreakMidNumbers.html"), new FileInfo(destinationFolder
                                                                                                             + "wordBreakMidNumbers.pdf"), converterProperties);
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + "wordBreakMidNumbers.pdf"
                                                                             , sourceFolder + "cmp_wordBreakMidNumbers.pdf", destinationFolder));
        }
Beispiel #6
0
        public virtual void OverflowXWordBreakTest()
        {
            // TODO: update cmp file after implementing DEVSIX-1438
            FontProvider fontProvider = new DefaultFontProvider();

            fontProvider.AddFont(fontsFolder + "NotoSansCJKjp-Regular.otf");
            ConverterProperties converterProperties = new ConverterProperties();

            converterProperties.SetFontProvider(fontProvider);
            HtmlConverter.ConvertToPdf(new FileInfo(sourceFolder + "overflowXWordBreak.html"), new FileInfo(destinationFolder
                                                                                                            + "overflowXWordBreak.pdf"), converterProperties);
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + "overflowXWordBreak.pdf"
                                                                             , sourceFolder + "cmp_overflowXWordBreak.pdf", destinationFolder));
        }
        public static void generatePDF()
        {
            // Base URI is required to resolve the path to source files
            ConverterProperties converterProperties = new ConverterProperties();

            using (var htmlStream = File.OpenRead(SRC))
            {
                if (File.Exists(DEST))
                {
                    File.Delete(DEST);
                }
                using (var pdfStream = File.OpenWrite(DEST))
                {
                    FontProvider fontProvider = new DefaultFontProvider();
                    FontProgram  fontProgram  = FontProgramFactory.CreateFont(FONT);
                    fontProvider.AddFont(fontProgram);
                    converterProperties.SetFontProvider(fontProvider);
                    HtmlConverter.ConvertToPdf(htmlStream, pdfStream, converterProperties);
                }
            }
        }
Beispiel #8
0
        public void ProcessRequest(HttpContext context)
        {
            string[] FONTS =
            {
                // msjh.ttc[0], the first one ttf in msjh.ttc.
                @"C:\Windows\Fonts\msjh.ttc,0",
            };
            // If the POST body has uncoded symbols,such as "<" and ">",
            // it will be judged as potentially dangerous. And put the POST
            // body to Request.Unvalidated.Form.
            String HTML = context.Request.Unvalidated.Form["content"] == null ? context.Request.Form["content"]: context.Request.Unvalidated.Form["content"];

            // Check whether the HTML has content.
            if (HTML != null && HTML != "")
            {
                // Define the date as the pdf file name.
                String strDate  = DateTime.Now.ToString("yyyyMMddHHmmss");
                String filename = "D:\\" + strDate + ".pdf";

                ConverterProperties properties = new ConverterProperties();
                // Deal with font provider.
                FontProvider fontProvider = new DefaultFontProvider(false, false, false);
                foreach (string font in FONTS)
                {
                    FontProgram fontProgram = FontProgramFactory.CreateFont(font);
                    fontProvider.AddFont(fontProgram);
                }
                // Set font type
                properties.SetFontProvider(fontProvider);
                // Set the base uri (that is, the root) of the website.
                // HttpContext.Current.Server.MapPath("~") : the root of website.
                properties.SetBaseUri(HttpContext.Current.Server.MapPath("~"));

                // Convert html to pdf.
                HtmlConverter.ConvertToPdf(WebUtility.HtmlDecode(HTML), new FileStream(filename, FileMode.Create), properties);
                Download(filename, strDate + ".pdf");
            }
        }
Beispiel #9
0
        public virtual void ComparatorErrorTest()
        {
            // TODO: DEVSIX-4017 (Combination of default and pdfCalligraph fonts with italic style and '"courier new", courier,
            // monospace' family reproduces comparator exception. Update test after fixing.)
            ConverterProperties properties = new ConverterProperties();
            FontProvider        pro        = new DefaultFontProvider();

            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSansArabic-Regular.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSansArabic-Bold.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSansGurmukhi-Regular.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSansGurmukhi-Bold.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSansMyanmar-Regular.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSansMyanmar-Bold.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSansOriya-Regular.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSansOriya-Bold.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSerifBengali-Regular.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSerifBengali-Bold.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSerifDevanagari-Regular.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSerifDevanagari-Bold.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSerifGujarati-Regular.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSerifGujarati-Bold.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSerifHebrew-Regular.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSerifHebrew-Bold.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSerifKannada-Regular.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSerifKannada-Bold.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSerifKhmer-Regular.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSerifKhmer-Bold.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSerifMalayalam-Regular.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSerifMalayalam-Bold.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSerifMyanmar-Regular.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSerifMyanmar-Bold.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSerifTamil-Regular.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSerifTamil-Bold.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSerifTelugu-Regular.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSerifTelugu-Bold.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSerifThai-Regular.ttf"));
            pro.AddFont(FontProgramFactory.CreateFont(SOURCE_FOLDER + "NotoSerifThai-Bold.ttf"));
            properties.SetFontProvider(pro);
            bool isExceptionThrown = false;

            try {
                HtmlConverter.ConvertToPdf(new FileInfo(SOURCE_FOLDER + "comparatorError.html"), new FileInfo(DESTINATION_FOLDER
                                                                                                              + "comparatorError.pdf"), properties);
            }
            catch (ArgumentException e) {
                NUnit.Framework.Assert.AreEqual("Comparison method violates its general contract!", e.Message);
                isExceptionThrown = true;
            }
            if (!isExceptionThrown)
            {
                NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(DESTINATION_FOLDER + "comparatorError.pdf"
                                                                                 , SOURCE_FOLDER + "cmp_comparatorError.pdf", DESTINATION_FOLDER));
            }
        }