# PDF Creation in slim framework

**URL:** <https://discourse.slimframework.com/t/pdf-creation-in-slim-framework/3857>\
**Category:** Questions\
**Created:** [February 13, 2020, 7:58am UTC](https://discourse.slimframework.com/t/pdf-creation-in-slim-framework/3857 "2020-02-13T07:58:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![skgp](https://avatars.discourse-cdn.com/v4/letter/s/9fc29f/32.png) [@skgp](https://discourse.slimframework.com/u/skgp)\
**Post date:** [February 13, 2020, 7:58am UTC](https://discourse.slimframework.com/t/pdf-creation-in-slim-framework/3857/1 "2020-02-13T07:58:27Z")

</div>

how to Done a PDF Creation in slim framework?

---

<div class="post-metadata">

**Author:** ![drmagorr](https://avatars.discourse-cdn.com/v4/letter/d/c2a13f/32.png) [@drmagorr](https://discourse.slimframework.com/u/drmagorr)\
**Post date:** [February 13, 2020, 9:11am UTC](https://discourse.slimframework.com/t/pdf-creation-in-slim-framework/3857/2 "2020-02-13T09:11:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [February 13, 2020, 9:19pm UTC](https://discourse.slimframework.com/t/pdf-creation-in-slim-framework/3857/3 "2020-02-13T21:19:57Z")

</div>

I’m using [TCPDF](https://tcpdf.org/)

---

<div class="post-metadata">

**Author:** ![skgp](https://avatars.discourse-cdn.com/v4/letter/s/9fc29f/32.png) [@skgp](https://discourse.slimframework.com/u/skgp)\
**Post date:** [February 14, 2020, 7:48am UTC](https://discourse.slimframework.com/t/pdf-creation-in-slim-framework/3857/4 "2020-02-14T07:48:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [February 14, 2020, 12:45pm UTC](https://discourse.slimframework.com/t/pdf-creation-in-slim-framework/3857/5 "2020-02-14T12:45:14Z")

</div>

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

```php
$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');

$response->getBody()->write($content);

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

return $response;

```

### Sending a PDF to the client (browser)

```php
// 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);
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:

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

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

return $response;

```

---

<div class="post-metadata">

**Author:** ![russell](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/russell/32/1363_2.png) [@russell](https://discourse.slimframework.com/u/russell)\
**Post date:** [August 2, 2021, 8:54pm UTC](https://discourse.slimframework.com/t/pdf-creation-in-slim-framework/3857/6 "2021-08-02T20:54:50Z")

</div>

I use TCPDF and SetaPDF.

---

<div class="post-metadata">

**Author:** ![trendschau](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/trendschau/32/1250_2.png) [@trendschau](https://discourse.slimframework.com/u/trendschau)\
**Post date:** [August 6, 2021, 7:14am UTC](https://discourse.slimframework.com/t/pdf-creation-in-slim-framework/3857/7 "2021-08-06T07:14:08Z")

</div>

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](http://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/](https://printcss.live/) where you can experiment with some pdf-rendering engines…
