charuyasas's avatar

Pdf file direct print form server side

I’m developing a web system using Laravel for a printing shop. I have a major requirement to print PDF files directly from the server side without showing any pop-up windows. Additionally, there should be an option to select a specific printer and set the number of copies for each file. I have already implemented these features in the UI and used Laravel queue jobs to manage the PDF printing process.

However, I haven’t been able to find a way to actually print a PDF file from the server side as required. In the worst case, a single print job can include 20–30 PDF files, and each PDF file can contain 30–70 pages. Can anyone help me with this?

0 likes
4 replies
Jsanwo64's avatar

I think what you definatly need is CUPs. https://openprinting.github.io/cups/doc/overview.html

First you will need to have it installed

sudo apt install cups
sudo lpadmin -p MyPrinter -E -v socket://192.168.0.150 -m everywhere
sudo lpoptions -d MyPrinter

Print using

exec("lp -d MyPrinter -n 3 '/path/to/file.pdf'");

Then for the queue job, something like this;

public function handle()
{
    $printer = "MyPrinter";
    $copies = 2;
    $filePath = storage_path('app/pdfs/sample.pdf');

    $command = "lp -d {$printer} -n {$copies} '{$filePath}'";
    exec($command);
}
Tray2's avatar

A printer server is definately needed for something like this. One with an api the allows you register the print jobs.

We use VPSX for this at work, it has a super nice gui to register the printers.

alex458's avatar

Hey charuyasas! That’s a solid setup you’ve got going with Laravel and queue jobs — nice work. Unfortunately, web servers can’t directly send PDFs to a client-side printer for security reasons. But if you’re talking about printing from the server machine itself (like an on-premises setup in your shop), you can totally automate that.

You’d need a server-side printing tool or library. For Laravel, people often use PHP’s exec() to call a command-line printer utility like CUPS (on Linux) or Print Spooler with lp / lpr / gsprint on Windows. For example:

exec('lp -d "Printer_Name" -n 2 /path/to/file.pdf');

This would send the PDF straight to the printer without any pop-up. You can control printer selection and copies right from that command.

If you’re deploying on Windows, check out SumatraPDF or PDFtoPrinter.exe, both lightweight and work great for silent printing from PHP.

Please or to participate in this conversation.