mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-12 12:33:26 +00:00
Fix PDF generation for print
This commit is contained in:
@@ -35,17 +35,14 @@ class Printer implements ControllerProviderInterface
|
|||||||
$controllers->post('/print.pdf', function (Application $app) {
|
$controllers->post('/print.pdf', function (Application $app) {
|
||||||
$printer = new RecordHelper\Printer($app, $app['request']);
|
$printer = new RecordHelper\Printer($app, $app['request']);
|
||||||
|
|
||||||
$request = $app['request'];
|
$layout = $app['request']->request->get('lay');
|
||||||
|
|
||||||
$layout = $request->request->get('lay');
|
|
||||||
|
|
||||||
foreach ($printer->get_elements() as $record) {
|
foreach ($printer->get_elements() as $record) {
|
||||||
$app['phraseanet.logger']($record->get_databox())
|
$app['phraseanet.logger']($record->get_databox())->log($record, \Session_Logger::EVENT_PRINT, $layout, '');
|
||||||
->log($record, \Session_Logger::EVENT_PRINT, $layout, '');
|
|
||||||
}
|
}
|
||||||
$PDF = new PDFExport($app, $printer->get_elements(), $layout);
|
$PDF = new PDFExport($app, $printer->get_elements(), $layout);
|
||||||
|
|
||||||
$response = new Response($PDF->render(), 200, ['Content-Type' => 'application/pdf']);
|
$response = new Response($PDF->render(), 200, array('Content-Type' => 'application/pdf'));
|
||||||
$response->headers->set('Pragma', 'public', true);
|
$response->headers->set('Pragma', 'public', true);
|
||||||
$response->setMaxAge(0);
|
$response->setMaxAge(0);
|
||||||
|
|
||||||
|
@@ -31,7 +31,6 @@ class Printer extends RecordHelper
|
|||||||
parent::__construct($app, $Request);
|
parent::__construct($app, $Request);
|
||||||
|
|
||||||
$grep = function (\record_adapter $record) {
|
$grep = function (\record_adapter $record) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return $record->get_thumbnail()->get_type() == \media_subdef::TYPE_IMAGE ||
|
return $record->get_thumbnail()->get_type() == \media_subdef::TYPE_IMAGE ||
|
||||||
$record->get_preview()->get_type() == \media_subdef::TYPE_IMAGE;
|
$record->get_preview()->get_type() == \media_subdef::TYPE_IMAGE;
|
||||||
|
@@ -42,18 +42,18 @@ class PDF
|
|||||||
case self::LAYOUT_PREVIEWCAPTIONTDM:
|
case self::LAYOUT_PREVIEWCAPTIONTDM:
|
||||||
try {
|
try {
|
||||||
$subdef = $record->get_subdef('preview');
|
$subdef = $record->get_subdef('preview');
|
||||||
|
// fallback to thumbnail ( video, sound, doc ) ..
|
||||||
|
if ($subdef->get_type() !== \media_subdef::TYPE_IMAGE) {
|
||||||
|
$subdef = $record->get_thumbnail();
|
||||||
|
}
|
||||||
|
|
||||||
if (!$subdef->is_physically_present()) {
|
if (!$subdef->is_physically_present()) {
|
||||||
continue 2;
|
continue 2;
|
||||||
}
|
}
|
||||||
if ($subdef->get_type() !== \media_subdef::TYPE_IMAGE)
|
|
||||||
continue 2;
|
|
||||||
|
|
||||||
$subdef = $record->get_subdef('thumbnail');
|
if ($subdef->get_type() !== \media_subdef::TYPE_IMAGE) {
|
||||||
if (!$subdef->is_physically_present())
|
|
||||||
continue 2;
|
|
||||||
|
|
||||||
if ($subdef->get_type() !== \media_subdef::TYPE_IMAGE)
|
|
||||||
continue 2;
|
continue 2;
|
||||||
|
}
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
continue 2;
|
continue 2;
|
||||||
}
|
}
|
||||||
@@ -61,12 +61,14 @@ class PDF
|
|||||||
case self::LAYOUT_THUMBNAILLIST:
|
case self::LAYOUT_THUMBNAILLIST:
|
||||||
case self::LAYOUT_THUMBNAILGRID:
|
case self::LAYOUT_THUMBNAILGRID:
|
||||||
try {
|
try {
|
||||||
$subdef = $record->get_subdef('thumbnail');
|
$subdef = $record->get_thumbnail();
|
||||||
if (!$subdef->is_physically_present())
|
if (!$subdef->is_physically_present()) {
|
||||||
continue 2;
|
continue 2;
|
||||||
|
}
|
||||||
|
|
||||||
if ($subdef->get_type() !== \media_subdef::TYPE_IMAGE)
|
if ($subdef->get_type() !== \media_subdef::TYPE_IMAGE) {
|
||||||
throw new \Exception('Not suitable');
|
continue 2;
|
||||||
|
}
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
continue 2;
|
continue 2;
|
||||||
}
|
}
|
||||||
@@ -158,6 +160,10 @@ class PDF
|
|||||||
|
|
||||||
$subdef = $rec->get_subdef('preview');
|
$subdef = $rec->get_subdef('preview');
|
||||||
|
|
||||||
|
if ($subdef->get_type() !== \media_subdef::TYPE_IMAGE) {
|
||||||
|
$subdef = $rec->get_thumbnail();
|
||||||
|
}
|
||||||
|
|
||||||
$fimg = $subdef->get_pathfile();
|
$fimg = $subdef->get_pathfile();
|
||||||
|
|
||||||
if (!$this->app['acl']->get($this->app['authentication']->getUser())->has_right_on_base($rec->get_base_id(), "nowatermark")
|
if (!$this->app['acl']->get($this->app['authentication']->getUser())->has_right_on_base($rec->get_base_id(), "nowatermark")
|
||||||
@@ -224,16 +230,23 @@ class PDF
|
|||||||
$subdef = $rec->get_subdef('thumbnail');
|
$subdef = $rec->get_subdef('thumbnail');
|
||||||
|
|
||||||
$fimg = $subdef->get_pathfile();
|
$fimg = $subdef->get_pathfile();
|
||||||
|
|
||||||
$wimg = $himg = 50;
|
$wimg = $himg = 50;
|
||||||
|
// 1px = 3.77952 mm
|
||||||
|
$finalWidth = round($subdef->get_width() / 3.779528, 2);
|
||||||
|
$finalheight = round($subdef->get_height() / 3.779528, 2);
|
||||||
|
if ($finalWidth > 0 && $finalheight > 0) {
|
||||||
|
if ($finalWidth > $finalheight && ($wimg < $finalWidth))
|
||||||
|
$finalheight = $wimg * $finalheight / $finalWidth;
|
||||||
|
else if ($finalheight > $finalWidth && $himg < $finalheight)
|
||||||
|
$finalWidth = $himg * $finalWidth / $finalheight;
|
||||||
|
else if ($finalheight == $finalWidth && $himg < $finalheight) {
|
||||||
|
$finalheight = $wimg * $finalheight / $finalWidth;
|
||||||
|
$finalWidth = $himg * $finalWidth / $finalheight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ($subdef->get_width() > $subdef->get_height())
|
if ($this->pdf->GetY() > $this->pdf->getPageHeight() - (6 + $finalheight + 20))
|
||||||
$himg = round($wimg * $subdef->get_height() / $subdef->get_width());
|
|
||||||
else
|
|
||||||
$wimg = round($himg * $subdef->get_width() / $subdef->get_height());
|
|
||||||
|
|
||||||
$himg = 0;
|
|
||||||
|
|
||||||
if ($this->pdf->GetY() > $this->pdf->getPageHeight() - (6 + $himg + 20))
|
|
||||||
$this->pdf->AddPage();
|
$this->pdf->AddPage();
|
||||||
|
|
||||||
$title = "record : " . $rec->get_title();
|
$title = "record : " . $rec->get_title();
|
||||||
@@ -270,8 +283,8 @@ class PDF
|
|||||||
|
|
||||||
if ($fimg) {
|
if ($fimg) {
|
||||||
$y = $this->pdf->GetY();
|
$y = $this->pdf->GetY();
|
||||||
$this->pdf->Image($fimg, $lmargin, $y, $wimg, $himg);
|
$this->pdf->Image($fimg, $lmargin, $y, $finalWidth, $finalheight);
|
||||||
$this->pdf->SetY($y);
|
$this->pdf->SetY($y + 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
$nf = 0;
|
$nf = 0;
|
||||||
@@ -290,8 +303,8 @@ class PDF
|
|||||||
$this->pdf->Write(6, "\n");
|
$this->pdf->Write(6, "\n");
|
||||||
$nf++;
|
$nf++;
|
||||||
}
|
}
|
||||||
if ($this->pdf->PageNo() == $p0 && ($this->pdf->GetY() - $y0) < $himg)
|
if ($this->pdf->PageNo() == $p0 && ($this->pdf->GetY() - $y0) < $finalheight)
|
||||||
$this->pdf->SetY($y0 + $himg);
|
$this->pdf->SetY($y0 + $finalheight);
|
||||||
$ndoc++;
|
$ndoc++;
|
||||||
}
|
}
|
||||||
$this->pdf->SetLeftMargin($lmargin);
|
$this->pdf->SetLeftMargin($lmargin);
|
||||||
@@ -309,10 +322,7 @@ class PDF
|
|||||||
$this->pdf->AddPage();
|
$this->pdf->AddPage();
|
||||||
|
|
||||||
if ($withtdm === "CALCPAGES") {
|
if ($withtdm === "CALCPAGES") {
|
||||||
if ($presentationpage)
|
$rec->set_number($this->pdf->PageNo());
|
||||||
$rec->set_number($this->pdf->PageNo() + 1);
|
|
||||||
else
|
|
||||||
$rec->set_number($this->pdf->PageNo());
|
|
||||||
}
|
}
|
||||||
$lmargin = $this->pdf->GetX();
|
$lmargin = $this->pdf->GetX();
|
||||||
$himg = 0;
|
$himg = 0;
|
||||||
@@ -413,10 +423,14 @@ class PDF
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$y = $this->pdf->GetY() + 3;
|
$y = $this->pdf->GetY() + 5;
|
||||||
|
|
||||||
$subdef = $rec->get_subdef('preview');
|
$subdef = $rec->get_subdef('preview');
|
||||||
|
|
||||||
|
if ($subdef->get_type() !== \media_subdef::TYPE_IMAGE) {
|
||||||
|
$subdef = $rec->get_thumbnail();
|
||||||
|
}
|
||||||
|
|
||||||
$f = $subdef->get_pathfile();
|
$f = $subdef->get_pathfile();
|
||||||
|
|
||||||
if (!$this->app['acl']->get($this->app['authentication']->getUser())->has_right_on_base($rec->get_base_id(), "nowatermark")
|
if (!$this->app['acl']->get($this->app['authentication']->getUser())->has_right_on_base($rec->get_base_id(), "nowatermark")
|
||||||
@@ -424,19 +438,27 @@ class PDF
|
|||||||
$f = \recordutils_image::watermark($this->app, $subdef);
|
$f = \recordutils_image::watermark($this->app, $subdef);
|
||||||
|
|
||||||
$wimg = $himg = 150; // preview dans un carre de 150 mm
|
$wimg = $himg = 150; // preview dans un carre de 150 mm
|
||||||
if ($subdef->get_width() > 0 && $subdef->get_height() > 0) {
|
// 1px = 3.77952 mm
|
||||||
if ($subdef->get_width() > $subdef->get_height())
|
$finalWidth = round($subdef->get_width() / 3.779528, 2);
|
||||||
$himg = $wimg * $subdef->get_height() / $subdef->get_width();
|
$finalheight = round($subdef->get_height() / 3.779528, 2);
|
||||||
else
|
if ($finalWidth > 0 && $finalheight > 0) {
|
||||||
$wimg = $himg * $subdef->get_width() / $subdef->get_height();
|
if ($finalWidth > $finalheight && ($wimg < $finalWidth))
|
||||||
|
$finalheight = $wimg * $finalheight / $finalWidth;
|
||||||
|
else if ($finalheight > $finalWidth && $himg < $finalheight)
|
||||||
|
$finalWidth = $himg * $finalWidth / $finalheight;
|
||||||
|
else if ($finalheight == $finalWidth && $himg < $finalheight) {
|
||||||
|
$finalheight = $wimg * $finalheight / $finalWidth;
|
||||||
|
$finalWidth = $himg * $finalWidth / $finalheight;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$this->pdf->Image($f, $lmargin, $y, $wimg, $himg);
|
|
||||||
|
$this->pdf->Image($f, (210 - $finalWidth) / 2, $y, $finalWidth, $finalheight);
|
||||||
|
|
||||||
if ($miniConv != NULL) {
|
if ($miniConv != NULL) {
|
||||||
foreach ($miniConv as $oneF)
|
foreach ($miniConv as $oneF)
|
||||||
unlink($oneF);
|
unlink($oneF);
|
||||||
}
|
}
|
||||||
$this->pdf->SetXY($lmargin, $y += ( $himg + 5));
|
$this->pdf->SetXY($lmargin, $y += ( $finalheight + 5));
|
||||||
|
|
||||||
$nf = 0;
|
$nf = 0;
|
||||||
if ($write_caption) {
|
if ($write_caption) {
|
||||||
|
Reference in New Issue
Block a user