Blog Detail

24

Mar
Directly Print PDF Docs and Raw Text with Laravel Printing cover image

arrow_back Directly Print PDF Docs and Raw Text with Laravel Printing

Laravel Printing allows your application to directly send PDF documents or raw text directly from a remote server to a printer on your local network. Receipts can also be printed by first generating the raw text via the Rawilk\Printing\Receipts\ReceiptPrinter class, and then sending the text as a raw print job via the Printing facade.

$printJob = Printing::newPrintTask()
    ->printer($printerId)
    ->file('path_to_file.pdf')
    ->send();

$printJob->id(); // the id number returned from 
the print server

Supported Print Drivers:

General Requirements

  • PHP 8.0 or greater
  • Laravel 8.0 or greater
  • A printer on your local network that you can print to and that your selected printer can access.
  • A receipt printer if you are printing receipts

Installation

You can easily install this package via composer by running this command in your terminal:

composer require rawilk/laravel-printing

You can publish the config file with this command:

php artisan vendor:publish --provider="Rawilk\Printing\PrintingServiceProvider" --tag="config"

Basic Usage

Most operations through this package can be done with the Printing facade.

Listing printers

You can fetch all available printers on your print server like this:

$printers = Printing::printers();

foreach ($printers as $printer) {
    echo $printer->name();
}

No matter which driver you utilize, each $printer object will be an instance of Rawilk\Printing\Contracts\Printer. More info on the printer object is here.

Finding a Printer

You can find a specific printer if you know the printer’s id:

Printing::find($printerId);

Default Printer

If you have a default printer id set in the config file, you can easily access the printer via the facade:

Printing::defaultPrinter(); // returns an instance of Rawilk\Printing\Contracts\Printer if the printer is found

// or for just the id
Printing::defaultPrinterId();

Creating a New Print Job

You can send jobs to a printer on your print server by creating a new print task:

Printing::newPrintTask()
    ->printer($printerId)
    ->file('path_to_file.pdf')
    ->send();

This package has a lot more features and many usage examples with code. If you want to learn more about this package you can visit its complete documentation and source code on Github.

Published at : 24-03-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project