Initial commit
This commit is contained in:
49
src/Pdf/PdfGenerator.php
Normal file
49
src/Pdf/PdfGenerator.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user