Converting html text to pdf

we are trying to generate PDF from HTML string. my html renders the page without any issue. but when i try to generate pdf its not using my base url.

Sample Code:

Syncfusion.HtmlConverter.HtmlToPdfConverter htmlConverter = new Syncfusion.HtmlConverter.HtmlToPdfConverter(HtmlRenderingEngine.WebKit);
WebKitConverterSettings settings = new WebKitConverterSettings();
settings.WebKitPath = Path.Combine(environment.ContentRootPath, "QtBinariesWindows");
htmlConverter.ConverterSettings = settings;
PdfDocument document = htmlConverter.Convert(sb.ToString(), baseUrl);
document.PageSettings.Margins.All = 50;
MemoryStream fileStream = new MemoryStream();
document.Save(fileStream);
document.Close(true);

Note: Attached html and pdf files.

Attachment: samplefiles_6e372900.zip

2 Replies

SG Sivaram Gunabalan Syncfusion Team January 13, 2022 11:41 AM UTC

Hi kasirajan, 

We are able to reproduce the reported issue with baseurl not used in Html string to PDF conversion with provided html file in WebKit rendering engine. Currently we are working on validating the issue on our end and we will update the further details on January 18th,2022. 

Regards, 
Sivaram G 



SG Sivaram Gunabalan Syncfusion Team January 18, 2022 01:01 PM UTC

Hi Kasirajan, 

On our further analysis, we need to provide base URL to load the external styles and scripts. We can use HTML URL to PDF conversion for without having external styles and script cases. So kindly try the URL to PDF conversion to render the styles for the provided HTML file. We have attached the code snippet and output document below for your reference. 

            HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit); 
            WebKitConverterSettings settings = new WebKitConverterSettings(); 
            settings.WebKitPath = Path.Combine(_hostingEnvironment.ContentRootPath, "QtBinariesWindows"); 
            settings.AdditionalDelay = 4000; 
            string htmlPath = Path.Combine(_hostingEnvironment.ContentRootPath, "Data/sample.html"); 
            htmlConverter.ConverterSettings = settings; 
            PdfDocument document = htmlConverter.Convert(htmlPath); 
            document.PageSettings.Margins.All = 50; 
            MemoryStream fileStream = new MemoryStream(); 
            document.Save(fileStream); 
            document.Close(true); 
            return File(fileStream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "WebkitOutput.pdf"); 


Kindly refer below UG link for more information about URL to PDF conversion 

Kindly try the provided solution on your end and let us know the result. 

Regards, 
Sivaram G 


Loader.
Up arrow icon