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.


composer require tecnickcom/tcpdf

Creating a PDF

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

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

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


$response = $response
    ->withHeader('Content-Type', 'application/pdf')
    ->withHeader('Content-Disposition', 'attachment; filename="filename.pdf"');

return $response;

Sending a PDF to the client (browser)

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

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

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

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);

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

return $response;
1 Like

I use TCPDF and SetaPDF.

For easy stuff like invoices I use domPDF (because of LPGL-licence and it is pretty lightweight). In other context I used wkhtmltopdf with a php-wrapper, but that requires a binary on the server. I think all these solutions have limited css-capacities, so if you want to create a pdf-version of a website with all styles then you might run into problems.

Not sure about your usecase, but for an ebook-project I work with the script pagedjs.org. It is a JS-polyfill for paged media css. It only works in chrome, but they also provide a headless version (never tried it). The output is highly professional with running headers, footers, footnotes and more. There is a pretty cool tool called https://printcss.live/ where you can experiment with some pdf-rendering engines…