3

Problem:

I am passing HTML and creating pdf through ABC pdf.

But the CSS are not applied on the content and pdf created is not as expected.

Here is my code can u please suggest what is the problem or how we can apply CSS...

    public static String CreateHtmlFile(String strHtmlCode)
    {
        String Modifiedhtml = @"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd""><html class="" _Telerik_IE9"" xmlns=""http://www.w3.org/1999/xhtml"">" + strHtmlCode;
        Modifiedhtml = Modifiedhtml.Remove(Modifiedhtml.IndexOf(@"//<![CDATA["), (Modifiedhtml.IndexOf("//]]>") - Modifiedhtml.IndexOf(@"//<![CDATA[")));
        string[] stringSeparators = new string[] { "PdfCreator" };
        var baseUrl = HttpContext.Current.Request.Url.AbsoluteUri.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries).First();
        Modifiedhtml = Modifiedhtml.Replace(@"href=""../", (@"href=""" + baseUrl));
        Modifiedhtml = Modifiedhtml.Replace(@"href=""/", (@"href=""" + baseUrl));
        Doc theDoc = new Doc();
        theDoc.HtmlOptions.UseScript = false;
        //theDoc.Width = 1125;
        String s = string.Empty;
        //s = File.ReadAllText(@"D:\test.html");
        theDoc.Page = theDoc.AddPage();
        int theID;
        theID = theDoc.AddHtml(strHtmlCode);
        //theID = theDoc.AddHtml(s);
        while (true)
        {
            theDoc.FrameRect(); // add a black border
            if (!theDoc.Chainable(theID))
                break;
            theDoc.Page = theDoc.AddPage();
            theID = theDoc.AddImageToChain(theID);
        }
        for (int i = 1; i <= theDoc.PageCount; i++)
        {
            theDoc.PageNumber = i;
            theDoc.Flatten();
        }
        theDoc.Save(@"D:\two\pagedhtml4.pdf");
        theDoc.Clear();
        return String.Empty;
    }

strHtmlCode is the HTML of the page which we have to convert in PDF.

Thanks in advance

Anirudh Agarwal
  • 655
  • 2
  • 7
  • 29
  • Might not be useful in this context, but I recommend looking into phantomJS as a way to create pdfs. It's essentially a headless browser that renders the page with the option of creating a pdf – TGH Nov 18 '13 at 05:20
  • I may be wrong, but doesn't ABC pdf require that the relevant CSS be embedded in the HTML you are transforming? In other words, I'm not sure it actually follows URL references. – NotMe Nov 18 '13 at 05:20
  • ABC pdf is commercial and they provide tech support also so you should go there first. – Govind Malviya Nov 18 '13 at 05:21
  • Can u provide me some supporting links where i can learn how to create pdf from phantom js – Anirudh Agarwal Nov 18 '13 at 05:59

2 Answers2

5

From the WebSupergoo doc page on the AddHtml Function:

Adds a block of HTML styled text to the current page.

HTML styled text does not support CSS. For full featured, standard CSS, you want AddImageHtml.

Ross Presser
  • 6,027
  • 1
  • 34
  • 66
0

You are passing strHtmlCode into the AddHtml function. It looks like you really want to pass in Modifiedhtml instead.

Scott
  • 21,211
  • 8
  • 65
  • 72
a688
  • 1