With C# Use Chrome to Covert HTML to Pdf

With C# use Chrome to covert HTML to PDF

I don't know why it doesn't allow me to do that. But you can start a powershell instance and run it through powershell:

var process = new System.Diagnostics.Process();
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
var chrome = Path.Combine(Environment.GetEnvironmentVariable("ProgramFiles(x86)"), @"Google\Chrome\Application\chrome.exe");

// use powershell
process.StartInfo.FileName = "powershell";
// set the Chrome path as local variable in powershell and run
process.StartInfo.Arguments = "$chrome='" + chrome + @"'; & $chrome --headless --print-to-pdf='c:\Users\" + Environment.UserName + @"\desktop\myReport.pdf' https://google.com";
process.Start();

Chrome Headless PrintToPDF with HTML String or File?

After much testing, I see that I can pass in the path to an HTML file where it accepts a web URL. As far as I can find this isn't documented anywhere, but it does work as long as the path is formatted correctly. This is only tested on Windows (11), but I believe it works on MacOS as well, as long as all paths are correct.

System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo.FileName = @"C:\Program Files\Google\Chrome\Application\chrome.exe";

string arguments = @"--headless --disable-gpu --print-to-pdf=""C:\path\to\outputPDFFile.pdf"" ""C:\path\to\sourceHTMLFile.html""";
process.StartInfo.Arguments = arguments;
process.Start();

In order for this to work, the --headless flag needs to be present otherwise it will just open the file in your browser.

Other output options can be added with other arguments.
See here for examples.

How to get chrome headless output to memory efficiently with C#?

Quit using chrome through the command-line interface and use Chrome web drivers on C# like Selenium or Puppeteer instead. For Selenium, use the following NuGet:

https://www.nuget.org/packages/Selenium.WebDriver/4.0.0-rc2

Then you can print your HTML into PDF using the following code:

// Base 64 encode
var textBytes = Encoding.UTF8.GetBytes(html);
var b64Html = Convert.ToBase64String(textBytes);

// Create driver
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments(new List<string> { "no-sandbox", "headless", "disable-gpu" });
using var driver = new ChromeDriver(webdriverPath, chromeOptions);
// Little bit magic here. Refer to: https://stackoverflow.com/a/52498445/7279624
driver.Navigate().GoToUrl("data:text/html;base64," + b64Html);

// Print
var printOptions = new Dictionary<string, object> {
// Docs: https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF
{ "paperWidth", 210 / 25.4 },
{ "paperHeight", 297 / 25.4 },
};
var printOutput = driver.ExecuteChromeCommandWithResult("Page.printToPDF", printOptions) as Dictionary<string, object>;
var document = Convert.FromBase64String(printOutput["data"] as string);

A best Html to PDF converter in ASP.NET Core 6.0

You can use Syncfusion to convert html to pdf.

First, Install Syncfusion.HtmlToPdfConverter.Net.Windows NuGet package.

Then, Edit the corresponding method in the controller:

public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult ExportToPDF()
{
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

//Convert URL to PDF document
PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com");

//Create memory stream
MemoryStream stream = new MemoryStream();

//Save the document
document.Save(stream);

return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "HTML-to-PDF.pdf");
}
}

Don't forget to add the namespace:

using Syncfusion.Pdf;
using Syncfusion.HtmlConverter;

Finally, Add the call in Index.cshtml:

@{
Html.BeginForm("ExportToPDF", "Home", FormMethod.Post);
{
<div>
<input type="submit" value="Convert HTML to PDF" style="width:250px;height:27px" />
</div>
}
Html.EndForm();
}

You can change the URL Page you want to download in the controller method:

PdfDocument document = htmlConverter.Convert("Your Url");

For more details, you can refer to this document.

Update:

Yes, it's not actually open source, you need to get a license key to remove it (only 30 days free trial).

Free and effective first choice is actually SelectPDF.

If you really don't want to use it, you can choose the following two:

PuppeteerSharp:
It's a headless chrome instance which can capture pdfs, and has a nice C# library (Puppeteer Sharp).

jsPDF:
This is also a conversion tool used by open source.

Just my opinion, I think the experience of these two is not as good as SelectPDF. Maybe there are other free and up-to-date PDF converters, but I haven't found them and haven't used them.

How to convert HTML C# code to pdf, using api2pdf/htmlagilitypack

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
});

Use .net to automate Google Chrome Print to PDF feature

Sounds like overkill. I would write you a little viewer to view the result HTML (using Internet Explorer API, or WebKit). Then API to the PDFCreator COM library to convert it to a PDF with a click of a button...

http://pdfcreator.svn.sourceforge.net/viewvc/pdfcreator/trunk/COM/Samples/Dot%20Net/VS2005/C%23/Sample1/Form1.cs?revision=1276&view=markup

http://webkitdotnet.sourceforge.net/

Good luck!

Can PDFSharp create Pdf file from a Html string in Net Core?

I recently ran into the exact same issue myself. There is currently extremely limited ways in dealing with PDFs in general in .NET Core. To go through them...

  • PDF Sharp - Does not support .NET Core/Standard.
  • SelectPDF - Does have a "free" community version hidden in their website footer. Should be useable in most cases. https://selectpdf.com/community-edition/
  • IronPDF - "Enterprise" pricing. Starts at $1.5k
  • WKHTMLTOPDF - This is actually just an executable that someone has written a C# wrapper over the top to run the exe. Not a great solution.
  • iTextSharp - Has "hidden" pricing but apparently this is the only one that specifically will run on Linux under .NET Core (If that's important to you).

IMO the only free one that will do what you need is SelectPDF. And that's saying something because I don't rate the library or the API. But it's free and it works.

More info : https://dotnetcoretutorials.com/2019/07/02/creating-a-pdf-in-net-core/



Related Topics



Leave a reply



Submit