Print raw text receipts
mainRawilk\Printing\Receipts\ReceiptPrinter class. Once the raw text is generated, you can send it as a raw print job using the Printing facade.repository·main·Indexed 20 days ago
https://github.com/rawilk/laravel-printingA Laravel package that enables remote servers to send PDF documents, URLs, or raw text directly to printers on a local network. It supports PrintNode and CUPS drivers, allows for custom driver implementation via the Rawilk\Printing\Contracts\Driver interface, and includes a ReceiptPrinter class for generating formatted receipt text.
Rawilk\Printing\Receipts\ReceiptPrinter class. Once the raw text is generated, you can send it as a raw print job using the Printing facade.In version 4.0, all custom exceptions thrown by the package now either extend Rawilk\ Printing\\Exceptions\\PrintingException or implement the Rawilk\\Printing\\Exceptions\\ExceptionInterface. You can use these to catch any exception thrown by the package in a single try/catch block.
PrintJob represents a task sent to a physical printer via a print server. In this package, every print job object returned by a Driver must implement the Rawilk\Printing\Contracts\PrintJob contract. This ensures a consistent interface regardless of which driver (e.g., CUPS) is being used.Rawilk\Printing\Receipts\ReceiptPrinter class, and then dispatch that text as a raw print job via the Printing facade.When using the CUPS driver, the Rawilk\Printing\Drivers\Cups\Entity\PrintJob object provides additional CUPS-specific information beyond the standard print job interface.
Key CUPS-specific features:
id() method returns a URI pointing to the specific job on your CUPS server.job() method returns the underlying Rawilk\Printing\Api\Cups\Resources\PrintJob_ resource instance.// Example of accessing CUPS-specific print job data
$jobId = $cupsPrintJob->id(); // Returns a URI
$resource = $cupsPrintJob->job(); // Returns Rawilk\Printing\Api\Cups\Resources\PrintJob_When using the CUPS driver, the Rawilk\Printing\Drivers\Cups\Entity\Printer object provides additional CUPS-specific information beyond the standard printer interface.
Key CUPS-specific features:
id() method returns a URI pointing to the printer on your CUPS server, rather than a simple string ID.printer() method returns the underlying Rawilk\Printing\Api\Cups\Resources\Printer_ resource instance.// Example of accessing CUPS-specific printer data
$printerId = $cupsPrinter->id(); // Returns a URI
$resource = $cupsPrinter->printer(); // Returns Rawilk\Printing\Api\Cups\Resources\Printer_The ReceiptPrinter implements the Conditionable interface, allowing you to conditionally chain methods using the when method. This is useful for adding optional receipt elements (like tax lines or discount notices) based on application state.
$receipt = (string) (new ReceiptPrinter)
->text('foo')
->when(
$someCondition === true,
fn (ReceiptPrinter $printer) => $printer->centerAlign()
);The PrintNode API implementation is organized into Service classes. Each service handles specific API endpoints based on the resource being managed (e.g., printers, computers, printJobs).
Resources are the objects returned by these services (e.g., a printer object or a computer object). When using the Printing facade, these resource objects are wrapped in entity objects that maintain a reference to their underlying API resource.
Example of using the printers service to retrieve all printers:
$client->printers->all();Printer represents a physical printer on your print server. Every printer object returned from a Driver must implement the Rawilk\Printing\Contracts\Printer contract. This abstraction allows you to interact with different types of printers (e.g., via CUPS) using a consistent interface.The CUPS implementation organizes requests into Service classes. Each service handles specific types of interactions with the CUPS server.
$client->printers). Use these to perform actions like retrieving all printers or creating print jobs.Printing facade, CUPS entity objects will contain references to their relevant CUPS resource objects.// Example: Using the printers service to retrieve all printers
$client->printers->all();Instead of forking the package or extending classes to add custom behavior, you can use Laravel's macroable pattern to add new methods to existing classes in the laravel-printing package. This is useful for adding specialized logic to API clients, drivers, or entities without modifying the package source.
// Example pattern for adding a macro
// Note: The specific implementation depends on the class you are targeting
Rawilk\Printing\Printing::macro('myCustomMethod', function () {
// Your custom logic here
});You can configure your PrintNode API key using one of two methods:
For standard single-tenant applications, add the key to your .env file:
PRINT_NODE_API_KEY=your-api-keyIn multi-tenant applications where each tenant has unique credentials, omit the key from your .env file (or set it to null) and set it at runtime using the PrintNode class.
use Rawilk\Printing\Api\PrintNode\PrintNode;
PrintNode::setApiKey('your-api-key');