We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

WebKitHtmlConverter.Convert for HTML string doesn't load CSS, and reports incorrect output size

I am working with the WebKitHtmlConverter. In my project, I have to dynamically build a PDF with multiple data areas, some of which are HTML-sourced. I am finding that whenever I am rendering the HTML to the PDF no matter what I do, I cannot get the converter to also load linked CSS, even when I provide a valid baseUrl. In testing, if I try to load from a url (instead of an HTML string), it appears to load and apply the CSS just fine. The method I use is shown below. Am I doing something wrong to get the rendered HTML string to be styled? I've attached an example of the html that I'm trying to load with external styling.

Also, once the rendering is completed (for external CSS html strings), I am finding that the returned PdfLayoutResult is reporting the wrong Bounds (usually much larger than it should be). Also, many times extra pages are added to the PdfDocument as well. Any thoughts on this?

private PdfLayoutResult RenderHtmlToPage(PdfPage currentPage, string html)
        {
            var clientSize = currentPage.GetClientSize();

            //set up html generation
            var unitConv = new PdfUnitConvertor();
            float htmlWidth = unitConv.ConvertToPixels(clientSize.Width, PdfGraphicsUnit.Point);
            float htmlHeight = unitConv.ConvertToPixels(clientSize.Height, PdfGraphicsUnit.Point);
            var metafileFormat = new PdfMetafileLayoutFormat() { Layout = PdfLayoutType.Paginate, Break = PdfLayoutBreakType.FitPage };
            WebKitHtmlConverter htmlConv = new WebKitHtmlConverter() { EnableHyperlinks = true, EnableJavaScript = true };
            htmlConv.WebKitPath = Server.MapPath("QtBin");

            //generate html
            string htmlStr = ReplaceUnsupportedHtmlCharactersForRender(html).Trim();
            string baseUrlStr = string.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Authority);
            var mainContentConvResult = htmlConv.Convert(htmlStr, baseUrlStr, (int)htmlWidth, (int)htmlHeight);
            PdfLayoutResult layoutResult = null;
            mainContentConvResult.Render(currentPage, metafileFormat, out layoutResult);
            return layoutResult;
        }

Attachment: Test_529971cc.zip

5 Replies

CM Chinnu Muniyappan Syncfusion Team August 5, 2015 10:10 AM UTC

Hi David,
Thank you for using syncfusion products.
We were able to reproduce the problem and have logged defect report regarding this. This will be included in our upcoming Volume 3 release which will expected to be rolled out in the month of September. We have also created an internal incident to have follow up on this.
Please let us know if you need any further assistance on this.
Regards,
Chinnu


DA David Aschliman September 4, 2015 02:37 PM UTC

Is there an update regarding the specific date the Volume 3 Release will be available?

Thanks,
David


CM Chinnu Muniyappan Syncfusion Team September 7, 2015 06:50 AM UTC

Hi David,
The release of Volume 3 is tentatively scheduled to be in the last week of September 2015.
Regards,
Chinnu


DA David Aschliman October 5, 2015 08:04 PM UTC

I was able to verify that some of the issues that I was experiencing are fixed--namely, the loading external css and images. However, I am still having an issue with blank pages and incorrect PdfLayoutResult bounds. I've adjusted my method to only render the height needed for the HTML (by specifying -1). However, when I do this, the rendering emits an extra page again, and still seems to report the wrong height. If there is any clarification you need or if I'm doing something wrong, please let me know. Our client has been waiting for this fix, and will find this very disappointing.

private PdfLayoutResult RenderHtmlToPage(PdfPage currentPage, string html)
        {
            var clientSize = currentPage.GetClientSize();

            //set up html generation
            var unitConv = new PdfUnitConvertor();
            float htmlWidth = unitConv.ConvertToPixels(clientSize.Width, PdfGraphicsUnit.Point);
            float htmlHeight = unitConv.ConvertToPixels(clientSize.Height, PdfGraphicsUnit.Point);
            var metafileFormat = new PdfMetafileLayoutFormat() { Layout = PdfLayoutType.Paginate, Break = PdfLayoutBreakType.FitPage };
            WebKitHtmlConverter htmlConv = new WebKitHtmlConverter() { EnableHyperlinks = true, EnableJavaScript = true };
            htmlConv.WebKitPath = Server.MapPath("QtBin");

            //generate html
            string htmlStr = HtmlScrubber.ReplaceUnsupportedHtmlCharactersForRender(html).Trim();
            string baseUrlStr = string.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Authority);
            var mainContentConvResult = htmlConv.Convert(htmlStr, baseUrlStr, (int)htmlWidth, -1);
            PdfLayoutResult layoutResult = null;
            mainContentConvResult.Render(currentPage, metafileFormat, out layoutResult);
            return layoutResult;
        }


CM Chinnu Muniyappan Syncfusion Team October 6, 2015 08:50 AM UTC

Hi David,

We were able to reproduce the problem and have logged defect report regarding this. A support incident to track the status of this defect has been created under your account. Please log on to our support website to check for further updates

https://www.syncfusion.com/account/login?ReturnUrl=%2fsupport%2fdirecttrac%2fincidents

Please let me know if you have any questions.

Regards,

Chinnu


Loader.
Live Chat Icon For mobile
Up arrow icon