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. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Thread ID:

Created:

Updated:

Platform:

Replies:

119817 Aug 4,2015 08:04 PM UTC Oct 6,2015 08:50 AM UTC ASP.NET MVC 5
loading
Tags: PDF
David Aschliman
Asked On August 4, 2015 08:04 PM UTC

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

Chinnu Muniyappan [Syncfusion]
Replied On 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

David Aschliman
Replied On September 4, 2015 02:37 PM UTC

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

Thanks,
David

Chinnu Muniyappan [Syncfusion]
Replied On 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

David Aschliman
Replied On 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;
        }

Chinnu Muniyappan [Syncfusion]
Replied On 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


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

or the page will be automatically redirected to sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;