chromelyapps / Chromely

Build Cross Platform HTML Desktop Apps on .NET using native GUI, HTML5, JavaScript, CSS, Owin, AspNetCore (MVC, RazorPages, Blazor)
MIT License
2.98k stars 280 forks source link

Howto: Print To PDF #303

Closed mattkol closed 3 years ago

mattkol commented 3 years ago

PrintToPDF.zip out.pdf

    public class DemoWindow : Window
    {
        public DemoWindow(IChromelyNativeHost nativeHost,
                      IChromelyConfiguration config,
                      ChromelyHandlersResolver handlersResolver)
            : base(nativeHost, config, handlersResolver)
        {
        }

        public CefBrowserHost BrowserHost
        {
            get
            {
                return Browser?.GetHost();
            }
        }
    }
    [ControllerProperty(Name = "PrintToPdfController")]
    public class PrintToPdfController : ChromelyController
    {
        private readonly IChromelyConfiguration _config;
        private readonly DemoWindow _window;

        /// <summary>
        /// Initializes a new instance of the <see cref="DemoController"/> class.
        /// </summary>
        public PrintToPdfController(IChromelyConfiguration config, IChromelyWindow window)
        {
            _config = config;
            _window = window as DemoWindow;

            RegisterCommand("/printtopdf", Print);
        }

        private void Print(IDictionary<string, string> obj)
        {
            _window?.BrowserHost?.PrintToPdf("out.pdf", new CefPdfPrintSettings(), new PrintToPdfCallback());

            //_window?.BrowserHost?.PrintToPdf("out.pdf", new CefPdfPrintSettings
            //{
            //    BackgroundsEnabled = true,
            //    HeaderFooterEnabled = false,
            //    Landscape = false,

            //    MarginType = CefPdfPrintMarginType.Custom,
            //    MarginBottom = 0,
            //    MarginTop = 0,
            //    MarginLeft = 0,
            //    MarginRight = 0,

            //    PageWidth = 210000,
            //    PageHeight = 297000
            //}, new PrintToPdfCallback());
        }

        class PrintToPdfCallback : CefPdfPrintCallback
        {
            protected override void OnPdfPrintFinished(string path, bool ok)
            {
                if (ok)
                {
                    //using (var testStream = new FileStream(path, FileMode.Open))
                    //{
                    // Do the printer work here.
                    // Or other tasks.
                    //}
                }
            }

            public void Dispose()
            {
            }
        }
    }
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            AppBuilder
            .Create()
            .UseWindow<DemoWindow>()
            .UseApp<DemoApp>()
            .Build()
            .Run(args);
        }
    }

    public class DemoApp : ChromelyBasicApp
    {
        public override void ConfigureServices(IServiceCollection services)
        {
            base.ConfigureServices(services);
            RegisterControllerAssembly(services, typeof(DemoApp).Assembly);
        }
    }

image