Free Support Forum - aspose.com

MVC Core render view as HTML string to PDF, will it render javascript and styles?

Quick question regarding the Aspose HTML to PDF.

I am building an Asp .net Core 3.x app that nees to be able to send views as emails. So I have found the functionality to get the outputted HTML from a view as a html string.

My question is, will Aspose.PDF be able to take this HTML string including javscripts etc. and render the string including all javascripts etc. into a pdf file?

Best regards
Bo

@bohoirup

You can convert HTML to PDF using Aspose.PDF and API will honor referenced CSS styles and images. However, we are afraid that JavaScript execution would not be possible at the moment as this functionality is under process at the moment. In case you can share a sample HTML with us, we will log a ticket in our issue tracking system and will share the ID with you.

@asad.ali
Will the Aspose.HTML be able to render the HTML to PDF and execute the javascript?
And if not, is there any other way with Aspose to do this?

@bohoirup

We are afraid that this feature is not implemented in Aspose.HTML yet. However, would you kindly share your sample HTML file which includes JavaScript. We will log a feature request accordingly and share the ID with you.

We apologize for the inconvenience.

@asad.ali

I am afraid I cannot share sample HTML, since this is not permitted. But I do no think you actually need a sample, it is just a question of the renderer also allows jquery etc. and renders the page including all scripts before saving it to PDF

best regards
Bo

@bohoirup

There are multiple issues logged before for different cases where used JavaScript was not being honored by the API during HTML to PDF Conversion. A similar ticket as PDFNET-42909 has been attached to this forum thread as well so that you will receive a notification once it is resolved.

Furthermore, we requested for a sample HTML because it would allow us to see what type of document you are trying to convert as earlier logged tickets already have some sample files with them which we will investigate and implement the functionality accordingly.

Hi @asad.ali

I have attached a sample HTML and PDF of what I am seeing using the conversion of HTML to PDF.
You will need to rename the .html.pdf because I could not upload .html files

I have tried to convert other more basic HTML (tables etc. with the same outcome.

Can you please tell me how to get the HTML to fit on the page of the PDF?

Best regards
Bo

notification.html.pdf (4.7 KB)
notification.pdf (45.6 KB)

@bohoirup

It seems like the HTML shared by you lacks the resources used in it e.g. CSS/Styles. Please check attached screenshot how it rendered on our side in browser.

renderedhtml.png (7.3 KB)

Please try to attach HTML with resources in .zip format so that we can test the scenario in our environment and address it accordingly. Also, you can try following code snippet with Aspose.PDF in order to render HTML content on a full page:

var htmlOptions = new HtmlLoadOptions(dataDir);
//htmlOptions.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
htmlOptions.IsRenderToSinglePage = true;
var document = new Document(dataDir + "equationBreakingHtml.html", htmlOptions);
document.Save(dataDir + "static.pdf");

Hi @asad.ali

I have changed my test to using cdn’s instead, so now all should be included.
I have attached both the html and the pdf output.

This is the c# code I am using:

        var body = _renderer.RenderPartialToStringAsync("~/Views/Test/TestAsposeView2.cshtml", "").Result;
        body = body.Insert(0, "<html><head><meta charset='utf-8' /><meta name='viewport' content='width=device-width, initial-scale=1.0' />" +
            "<meta http-equiv='X-UA-Compatible' content='IE=edge;' />" +
            "<title>Risklogic</title>" +
            "<body><div class='container body-content rlMainContent'>");
        body += "</div></body></html>";

        var htmlDoc = new Aspose.Html.HTMLDocument(body, ".");
        var options = new PdfRenderingOptions();
        string outputfile = @"C:\_BP\Development\AsposeTesting\notification2.pdf";
        var device = new PdfDevice(options, outputfile);
        var renderer = new HtmlRenderer();
        renderer.Render(device, htmlDoc);

notification.html.pdf (6.0 KB)
notification2.pdf (48.7 KB)

@asad.ali

I am sorry, I have put this case in the Apose.pdf and not in the Aspose.html product forum :-\

I am using Aspose.HTML at the moment to test this, is the Aspose.PDF better at this?

Best regards
Bo

@bohoirup

We tried to set margins as zero and page width to maximum size but HTML content was not getting fit in output PDF while using Aspose.HTML for .NET 20.12 and following code snippet:

string file = dataDir + "notification.html";
//File.WriteAllText(file, "");
Aspose.Html.Saving.PdfSaveOptions options = new Html.Saving.PdfSaveOptions();
options.PageSetup.AnyPage.Margin = new Html.Drawing.Margin(0);
options.PageSetup.AdjustToWidestPage = true;
using (var document = new Aspose.Html.HTMLDocument(file))
{
 Aspose.Html.Converters.Converter.ConvertHTML(document, options, dataDir + "outputByAsposeHTML.pdf");
}

Therefore, we have logged an issue as HTMLNET-2901 in our issue tracking system. We will further look into its details and keep you posted with the status of its correction. Please be patient and spare us some time.

We are sorry for the inconvenience.

Yes, we noticed that as well and moved your post to the respective forum category. Please note that Aspose.HTML is recommended for usage if you are dealing with HTML files as it is specialized for such cases. You can also use Aspose.PDF if you want to perform further manipulations in PDF files.