I'm trying to convert HTML code written in C# to a PDF but getting issues when trying to do so. I get an error that says cannot HTMLAgilityPack.HTMLDocument object to Api2PdfChromeHtmltoPDFRequest. Is there a way to do so with these 2 libraries, or any better solutions?
var html = String.Format(@"<!DOCTYPE html>
<html><h1> Hello world </h1> </html>");
var doc = new HtmlDocument();
doc.LoadHtml(html)
Api2Pdf.Api2PdfResult emailConversionResult = a2pClient.Chrome.HtmlToPdf(doc);
CodePudding user response:
According to the documentation, HtmlToPdf is expecting an ChromeHtmlToPdfRequest
object (https://github.com/Api2Pdf/api2pdf.dotnet/blob/ba7da6496a45e1e07627158bc76d9ea48cdc6255/Api2Pdf.DotNet/RequestModels.cs#L42)
This is also what the error is trying to tell you. You are passing in an object of type HTMLAgilityPack.HTMLDocument
, yet the HtmlToPdf method is expecting an object of type ChromeHtmlToPdfRequest
. The compiler then complains that it cannot convert the first to the latter and throws an error at you.
So your code should look like this:
var emailConversionResult = a2pClient.Chrome.HtmlToPdf(new ChromeHtmlToPdfRequest
{
Html = html
});
Html
here is a string. So if you must use HtmlAgilityPack for some reason, you can also use this snippet:
var emailConversionResult = a2pClient.Chrome.HtmlToPdf(new ChromeHtmlToPdfRequest
{
Html = doc.DocumentNode.OuterHtml
});