Printing for Laravel

repository·main·Indexed 20 days ago

https://github.com/rawilk/laravel-printing

A 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.

Tokens
19.9K
Snippets
78
Records
108
Agent score
68%

What's inside laravel-printing

  1. Catch package exceptions in v4.0

    main

    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.

  2. Understand the PrintJob abstraction

    main
    A 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.
  3. Understand CUPS PrintJob entities

    main

    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:

    • The id() method returns a URI pointing to the specific job on your CUPS server.
    • The 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_
  4. Understand CUPS Printer entities

    main

    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:

    • The id() method returns a URI pointing to the printer on your CUPS server, rather than a simple string ID.
    • The 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_
  5. Use conditional logic when building receipts

    main

    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()
        );
  6. Use PrintNode Services and Resources

    main

    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();
  7. Understand the Printer abstraction

    main
    In this package, a 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.
  8. Use CUPS Services and Resources

    main

    The CUPS implementation organizes requests into Service classes. Each service handles specific types of interactions with the CUPS server.

    • Services: Accessed via the client (e.g., $client->printers). Use these to perform actions like retrieving all printers or creating print jobs.
    • Resources: These are the objects returned by the services (e.g., a printer or a print job). When using the 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();
  9. Extend functionality using Macros

    main

    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
    });
  10. Configure the PrintNode API key

    main

    You can configure your PrintNode API key using one of two methods:

    Via Environment Variables

    For standard single-tenant applications, add the key to your .env file:

    PRINT_NODE_API_KEY=your-api-key

    Via Runtime Configuration

    In 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');