Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi!
I'm trying using PrintToPdfAsync to print html to PDF but it dosent work. I have written the following code which dosent work. Please help me how I can print HtML to PDF using CefSharp with headless mode for .NETCore


Best Regards
Merh

What I have tried:

C#
using System;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
using System.Threading.Tasks;
namespace CefSharp.OffScreen
{
    public static class Program
    {
        private static ChromiumWebBrowser browser;

        [STAThread]
        public static void Main()
        {

            HtmlToPdfHeadless();
	}
 	
        private static void HtmlToPdfHeadless()
        {
            const string testUrl = "https://www.google.com/";
            var settings = new CefSettings();
            Cef.Initialize(settings);
            browser = new ChromiumWebBrowser(testUrl);
            string path = "Test.pdf";
            PrintToPdfAsync(path);
            Cef.Shutdown();
        }
        public static async Task PrintToPdfAsync(string path)
        { 
             await browser.PrintToPdfAsync(path);
        }
    }
}
Posted
Updated 12-Mar-21 2:20am
v3
Comments
NotTodayYo 12-Mar-21 7:41am    
Why doesn't it work?

1 solution

If I was to take a wild guess, because you've not actually explained the error that you're having on the post (which you should), I'd say it's because you're calling an asynchronous method synchronously. You make a call to:
C#
public static async Task PrintToPdfAsync(string path)
{
     await browser.PrintToPdfAsync(path);
}

But you don't await this method, so this part:
C#
PrintToPdfAsync(path);
Cef.Shutdown();

Will call the method PrintToPdfAsync and then immediately call Cef.Shutdown();. You need to await the print method.
 
Share this answer
 
Comments
merh 12-Mar-21 9:54am    
Sorry I have changed to the following and it still dosent work.
Task<int> printToPdfAsync = PrintToPdfAsync(path);
int result = await printToPdfAsync;
Chris Copeland 12-Mar-21 10:34am    
What isn't working exactly? You'd need to make your "Main" method method async, make the "PrintToPdfAsync" method async, and await the entire chain. If you don't, what's going to happen is the application is going to close before it's had a chance to print the PDF.
merh 12-Mar-21 12:22pm    
Hi!My code crashes at return await browser.PrintToPdfAsync(path);

using System;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
using System.Threading.Tasks;
using System.Threading;

namespace CefTest
{
public class Program
{
private static ChromiumWebBrowser browser;

[STAThread]
public static async Task Main(string[] args)
{
await HtmlToPdfHeadless();
}

private static async Task HtmlToPdfHeadless()
{
const string testUrl = "https://www.google.com/";
var settings = new CefSettings();
Cef.Initialize(settings);
browser = new ChromiumWebBrowser(testUrl);
string path = "Test.pdf";
Task<bool> printToPdfAsync = PrintToPdfAsync(path);
var result = await printToPdfAsync;
}

public static async Task<bool> PrintToPdfAsync(string path)
{
return await browser.PrintToPdfAsync(path);

}
}


}
Chris Copeland 15-Mar-21 5:34am    
Have you tried debugging the code, stepping through and examining the exception that's occurring? One thing that comes to mind is that you're creating the ChromiumWebBrowser and telling it to load the URL, but you're not subscribing to the LoadingStateChanged event to make sure the page has finished loading before you try and print to PDF.

The exception details would provide more information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900