After a PDF is generated and downloaded I want to reload the view to get the flash success message.
But when I use a redirect no pdf is generated but the view is reloaded.
public function smakeOrders(?string $id = null): void
{
$smakeShop = $this->SmakeShops->get($id, [
'contain' => [],
]);
$this->loadComponent('SmakeAPI', [
'endpoint' => $smakeShop->endpoint,
'token' => $smakeShop->token,
]);
$orders = $this->SmakeAPI->getOrders($this->request->getAttribute('params'));
$ordersCount = count($orders);
$subtotal = $this->SmakeAPI->getTotal();
$subtotalVat = $this->SmakeAPI->getTotal(true);
$subtotalShipping = $this->SmakeAPI->getTotalShipping();
$subtotalShippingVat = $this->SmakeAPI->getTotalShipping(true);
if ($ordersCount > 0) {
$this->viewBuilder()->setClassName('CakePdf.Pdf');
$this->viewBuilder()->setOption(
'pdfConfig',
[
'download' => true,
'filename' => 'bestelllisten-nr-' .
FrozenTime::now()->format('Y') .
$smakeShop->document_number . '-' .
$smakeShop->company . '.pdf',
]
);
}
$smakeShop = $this->SmakeShops->patchEntity($smakeShop, [
'document_number' => $smakeShop->document_number + 1,
]);
$this->SmakeShops->save($smakeShop);
if ($ordersCount > 0) {
$this->Flash->success(__("Orders found: {$ordersCount} | PDF generated"));
} else {
$this->Flash->error(__('No Orders found'));
}
$this->set(['orders' => $orders,
'smakeShop' => $smakeShop,
'subtotal' => $subtotal,
'subtotalVat' => $subtotalVat,
'subtotalShipping' => $subtotalShipping,
'subtotalShippingVat' => $subtotalShippingVat,
'now' => FrozenTime::now()]);
// if this is set view gets reloaded but no pdf is generated
$this->redirect(['action' => 'view', $id]);
}
Does anyone have an idea or a hint how to implement this?