PDF Creation in slim framework

how to Done a PDF Creation in slim framework?

In Slim I never did, but in another PHP framework I use “mpdf”. https://github.com/mpdf/mpdf

I’m using TCPDF

kindly send a steps to do PDF
creation using TCPDF in slim

As long as you don’t want to download / stream the generated PDF file, there is no Slim specific step required.

Installation

composer require tecnickcom/tcpdf

Creating a PDF

$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false, true);

$pdf->addPage();
$pdf->write(0, 'My Test');

// Render and return pdf content as string
$content = $pdf->output('doc.pdf', 'S');

Sending a PDF to the client (browser)

// Send file to the browser as download
$fileName = 'example.pdf';

$response = $response->withType('application/pdf');
$response = $response->withHeader('Content-Disposition', sprintf('attachment; filename="%s"', $fileName));

$stream = fopen('php://memory', 'w+');
fwrite($stream, $content);
rewind($stream);

return $response->withBody(new \Slim\Http\Stream($stream));

If you want to return the entire content at once, you could use this workaround:

$stream = fopen('php://memory', 'w+');
fwrite($stream, $content);
rewind($stream);

$response->getBody()->write(fread($stream, (int)fstat($stream)['size']));

return $response;
1 Like