Add Printjs to infolist action

Hi, in my app i have an infolist with a button 'print'
 
#PRINT INVOICE
Action::make(__('repair.print'))
->action(fn ($data, Repair $record) => redirect()->to(
route('repair.pdf.'.$data['type'], $record)
))

->icon('heroicon-s-printer')
->form([
Forms\Components\Radio::make('type')
->label('')
->default('receipt')
->options(PrintOptions::class)
->disableOptionWhen(function (string $value, Model $record) {
return $value === 'aggregate' && $record->parts === null || $value === 'regular' && $record->parts === null;
})
])
 
#PRINT INVOICE
Action::make(__('repair.print'))
->action(fn ($data, Repair $record) => redirect()->to(
route('repair.pdf.'.$data['type'], $record)
))

->icon('heroicon-s-printer')
->form([
Forms\Components\Radio::make('type')
->label('')
->default('receipt')
->options(PrintOptions::class)
->disableOptionWhen(function (string $value, Model $record) {
return $value === 'aggregate' && $record->parts === null || $value === 'regular' && $record->parts === null;
})
])
this action all a route that generate and show a pdf in the browser (same tab) now i want to add printerjs in order to print it directly i've added print js with npm import in app.js compiled with npm run build and inspecting the code, the library seems to be loaded right now
9 Replies
Soundmit
SoundmitOP5mo ago
this is the route Route::get('/label/{record}/download', [PdfController::class, 'downloadLabel'])->name('repair.pdf.label'); that calls the controller
public function downloadLabel($record)
{
$repair = Repair::find($record);
$companyData = app(CompanySettings::class);
$width = $this->mmToPoints(54);
$height = $this->mmToPoints(25);
$customPaper = [0, 0, $width, $height];
$pdf = PDF::loadView('pdf.label', compact('repair', 'companyData'))
->setPaper($customPaper)
->setWarnings(false);

return $pdf->stream('label.pdf');
}
public function downloadLabel($record)
{
$repair = Repair::find($record);
$companyData = app(CompanySettings::class);
$width = $this->mmToPoints(54);
$height = $this->mmToPoints(25);
$customPaper = [0, 0, $width, $height];
$pdf = PDF::loadView('pdf.label', compact('repair', 'companyData'))
->setPaper($customPaper)
->setWarnings(false);

return $pdf->stream('label.pdf');
}
how can i modify the action to call printjs instead of open the pdf?
Dennis Koch
Dennis Koch5mo ago
Adding wire:click with the JS code to the extraAttributes might do it.
Soundmit
SoundmitOP5mo ago
ok i try
Soundmit
SoundmitOP5mo ago
I've already spent 3 hours on this. Is what I'm trying to do feasible, or am I wasting my time?
awcodes
awcodes5mo ago
So you’re using a livewire click handler with a js query selector,? There’s no way that’s ever going to work.
Soundmit
SoundmitOP5mo ago
any chance to use printjs? my app is working, but in the infolist when i print a label/invoice etc the pdf is opened in the same tab, after printing the pdf i have to hit the browser's back button to comback to the app.. not so friendly
awcodes
awcodes5mo ago
You probably need to run it through alpine, then use $wire to stream it back to the browser. Alpine will have access to the query selector. If that makes sense. But you’re getting a null either way on the query selector which means it can’t find the html element. But I think that’s because of the livewire click handler. Actions also have an ->alpineClickHandler() that will disable the livewire click handler. Maybe start there.
Soundmit
SoundmitOP5mo ago
Thank you, tomorrow I will try
Want results from more Discord servers?
Add your server