views:

54

answers:

1

I am developping an intranet ASP.NET MVC2 application for a small business. The small business has multiple type of printers and depending on what is required a print request will be sent (from the browser/user) to the server and the server will dispatch the print job to the right printer accordingly. Please note that it is a completely new environment for them and I have control over pretty much everything. I will more than likely use a very lightweight OS (maybe Asus ExpressGate or Chrome OS depending on launch date?) so the users cannot have any printers installed but the server will have everything setup.

Here is my question:

Is there a simple way to print from the server side (without dialog of course because there won't be anyone waiting to click them) a page by using the html link as a parameter and keeping the HTML format of course.

I have seen a few possibilities of COM stuff out there but if there are possibilities to avoid this by using a .net class I would appreciate. I am using .net 4.0. I will however take any suggestions even if it is COM based.

Edit: Please note that any workaround that makes sense would also be taken into consideration, a quick (non studied yet) example would be to transfer this html to a doc file and sending this file to the printer.

Edit-Code taken off for lack of use.

Edits2: Following this link: http://stackoverflow.com/questions/416314/print-html-document-from-windows-service-in-c-without-print-dialog

Vadim's holy grail solution DOES work. However, it has limitations like using the default printer only. I am modifying the default printer before the print occurs this causes the print to go to the right printer. I can see some concurrence issues happening here but so far this is the best i came up with (most of this code is from Vadim and I give him the full credit for this):

    /// <summary>Provides a scheduler that uses STA threads.</summary>
public sealed class StaTaskScheduler : TaskScheduler, IDisposable
{
    /// <summary>Stores the queued tasks to be executed by our pool of STA threads.</summary>
    private BlockingCollection<Task> _tasks;
    /// <summary>The STA threads used by the scheduler.</summary>
    private readonly List<Thread> _threads;

    /// <summary>Initializes a new instance of the StaTaskScheduler class with the specified concurrency level.</summary>
    /// <param name="numberOfThreads">The number of threads that should be created and used by this scheduler.</param>
    public StaTaskScheduler(int numberOfThreads)
    {
        // Validate arguments
        if (numberOfThreads < 1) throw new ArgumentOutOfRangeException("concurrencyLevel");

        // Initialize the tasks collection
        _tasks = new BlockingCollection<Task>();

        // Create the threads to be used by this scheduler
        _threads = Enumerable.Range(0, numberOfThreads).Select(i =>
        {
            var thread = new Thread(() =>
            {
                // Continually get the next task and try to execute it.
                // This will continue until the scheduler is disposed and no more tasks remain.
                foreach (var t in _tasks.GetConsumingEnumerable())
                {
                    TryExecuteTask(t);
                }
            });
            thread.IsBackground = true;
            thread.SetApartmentState(ApartmentState.STA);
            return thread;
        }).ToList();

        // Start all of the threads
        _threads.ForEach(t => t.Start());
    }

    /// <summary>Queues a Task to be executed by this scheduler.</summary>
    /// <param name="task">The task to be executed.</param>
    protected override void QueueTask(Task task)
    {
        // Push it into the blocking collection of tasks
        _tasks.Add(task);
    }

    /// <summary>Provides a list of the scheduled tasks for the debugger to consume.</summary>
    /// <returns>An enumerable of all tasks currently scheduled.</returns>
    protected override IEnumerable<Task> GetScheduledTasks()
    {
        // Serialize the contents of the blocking collection of tasks for the debugger
        return _tasks.ToArray();
    }

    /// <summary>Determines whether a Task may be inlined.</summary>
    /// <param name="task">The task to be executed.</param>
    /// <param name="taskWasPreviouslyQueued">Whether the task was previously queued.</param>
    /// <returns>true if the task was successfully inlined; otherwise, false.</returns>
    protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
    {
        // Try to inline if the current thread is STA
        return
            Thread.CurrentThread.GetApartmentState() == ApartmentState.STA &&
            TryExecuteTask(task);
    }

    /// <summary>Gets the maximum concurrency level supported by this scheduler.</summary>
    public override int MaximumConcurrencyLevel
    {
        get { return _threads.Count; }
    }

    /// <summary>
    /// Cleans up the scheduler by indicating that no more tasks will be queued.
    /// This method blocks until all threads successfully shutdown.
    /// </summary>
    public void Dispose()
    {
        if (_tasks != null)
        {
            // Indicate that no new tasks will be coming in
            _tasks.CompleteAdding();

            // Wait for all threads to finish processing tasks
            foreach (var thread in _threads) thread.Join();

            // Cleanup
            _tasks.Dispose();
            _tasks = null;
        }
    }
}
    public class PrinterHelper
{
    readonly TaskScheduler _sta = new StaTaskScheduler(1);
    public void PrintHtml(string htmlPath, string printerDevice)
    {
        if (!string.IsNullOrEmpty(printerDevice))
            SetAsDefaultPrinter(printerDevice);


        Task.Factory.StartNew(() => PrintOnStaThread(htmlPath), CancellationToken.None, TaskCreationOptions.None, _sta).Wait();
    }

    static void PrintOnStaThread(string htmlPath)
    {
        const short PRINT_WAITFORCOMPLETION = 2;
        const int OLECMDID_PRINT = 6;
        const int OLECMDEXECOPT_DONTPROMPTUSER = 2;

        using(var browser = new WebBrowser())
        {
            browser.Navigate(htmlPath);
            while(browser.ReadyState != WebBrowserReadyState.Complete)
                Application.DoEvents();

            dynamic ie = browser.ActiveXInstance;
            ie.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, PRINT_WAITFORCOMPLETION);
        }
    }


    static void SetAsDefaultPrinter(string printerDevice)
    {
        foreach (var printer in PrinterSettings.InstalledPrinters)
        {
            //Verify that the printer exists here
        }
        var path = "win32_printer.DeviceId='" + printerDevice + "'";
        using (var printer = new ManagementObject(path))
        {
            ManagementBaseObject outParams =
            printer.InvokeMethod("SetDefaultPrinter",
            null, null);
        }

        return;
    }

}
A: 

A couple of articles for readng which maybe helpful are:

Will update if I find anything else. Hope this helps.

WestDiscGolf
Yes I currently have a printer helper that I found on here in stackoverflow that I added as an edit above. Unfortunately, this doesn't help me print a HTML rendered file so far.
Nick-ACNB
I've not looked but is there a way to print a stream? If so then you could potentiall get the html created and then print the response stream which includes the html ... just a crazy thought :)
WestDiscGolf
Using the code that I show up there, it does work. For the need of this project it works because it is a small business. I would probably reconsider this was it something that was going to be used a lot more because of concurrence with changeing the default printer. I hate working with COM objects and this one requires that IE is installed on the server, but gotta do what you gotta do uh? I am still open to other more scalable suggestions!
Nick-ACNB