March 10, 2017

Magento Invoice Auto Printer

We recently had a fun project. We needed a quick and efficient method of printing off massive amounts of invoices automatically after long weekends.

The trick also was that we had a custom invoice PDF and needed to change the status of the actual order to “Invoice Printed”, while adding an additional order comment that the order had in fact been printed.

With a bit of C# code and two customer PHP files for integration with Magento, we had a quick executable file. Only user requirement was a windows workstation running Dot Net, adobe reader and a connected printer. When the user clicked the file, the program would call out to the Magento website asking for order to print. The business logic made sure it did not go back more the 15 days and only grab “Processing” orders. When the order batch is retrieved, the C# program called out to the custom PDF invoice generator, downloaded the PDF local to the user’s workstation, and silently opened it adobe sending it to the printer. After the PDF was completed, it would close Adobe, and call back to the custom script on the website, changing the order status to “Invoice Printed” along with a comment, so the order would not be re-printed (and other users know that it was printed already).

We are sure this could be developed in a full scale auto-printing solution, but that wasn’t the scope of our needs. Quick, simple and easy to use.

This is a snippet of our C# program.

public static void Main(string[] args)
			Console.WriteLine("Hello World!");
			AgilePrinter p=new AgilePrinter();
			//grab orders from  PHP script on website.
			String orders=p.getOrders();
			//split orders to parse (expects just a single nl
			string[] result = orders.Split(new string[] {"\r\n", "\n"}, StringSplitOptions.RemoveEmptyEntries);
			foreach(string order_id in result)
				Log("working on: "+order_id);
                                //link to our custom PDF invoice
				String url=""+order_id;
				//first run, we don't want to trigger the status
				string remoteFile=url+"&NO_STATUS=TRUE";
				Log(" Remote File: "+remoteFile);
				string localFile;
				Log(" Printing File: "+localFile);
				//if we done..recall the php file WITH status
				Log(" Calling again to change status: "+remoteFile);
			//Log("Press any key to continue . . . ");