49 lines
1.3 KiB
PHP
49 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace NoccyLabs\Bundle\PdfBundle\Pdf;
|
|
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
|
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
|
use Dompdf\Dompdf;
|
|
|
|
class PdfGenerator
|
|
{
|
|
protected $tempDir;
|
|
|
|
public function __construct($tempDir)
|
|
{
|
|
$this->tempDir = $tempDir;
|
|
}
|
|
|
|
public function generate($html, array $options=[])
|
|
{
|
|
$pdf = new Dompdf();
|
|
$pdf->loadHtml($html);
|
|
$pdf->render();
|
|
|
|
$output = $pdf->output();
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
public function generateResponseFromHtml($html, array $options=[])
|
|
{
|
|
$tempFile = $this->tempDir . "/" . "pdf_" . md5(uniqid()) . ".pdf";
|
|
|
|
// Call on generate with the options provided
|
|
$output = $this->generate($html, $options);
|
|
file_put_contents($tempFile, $output);
|
|
|
|
// Prepare the response
|
|
$response = new BinaryFileResponse($tempFile);
|
|
if (array_key_exists('filename', $options)) {
|
|
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $options['filename']);
|
|
} else {
|
|
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_INLINE);
|
|
}
|
|
$response->deleteFileAfterSend(true);
|
|
|
|
return $response;
|
|
}
|
|
} |