Fix PDF generation for print

This commit is contained in:
Nicolas Le Goff
2014-06-16 20:27:54 +02:00
parent a87c5bb8e4
commit c5b3e70ab0
4 changed files with 61 additions and 43 deletions

View File

@@ -39,17 +39,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, array('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);

View File

@@ -115,7 +115,7 @@ class Helper extends \Alchemy\Phrasea\Helper\Helper
if (trim($Request->get('ssel')) !== '') { if (trim($Request->get('ssel')) !== '') {
$repository = $app['EM']->getRepository('\Entities\Basket'); $repository = $app['EM']->getRepository('\Entities\Basket');
/* @var $$repository \Repositories\BasketRepository */ /* @var $repository \Repositories\BasketRepository */
$Basket = $repository->findUserBasket($this->app, $Request->get('ssel'), $app['authentication']->getUser(), false); $Basket = $repository->findUserBasket($this->app, $Request->get('ssel'), $app['authentication']->getUser(), false);
$this->selection->load_basket($Basket); $this->selection->load_basket($Basket);

View File

@@ -40,7 +40,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;

View File

@@ -48,18 +48,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;
} }
@@ -67,12 +67,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;
} }
@@ -164,6 +166,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['authentication']->getUser()->ACL()->has_right_on_base($rec->get_base_id(), "nowatermark") if (!$this->app['authentication']->getUser()->ACL()->has_right_on_base($rec->get_base_id(), "nowatermark")
@@ -233,16 +239,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();
@@ -279,8 +292,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;
@@ -299,8 +312,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);
@@ -318,10 +331,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();
$tmargin = $this->pdf->GetY(); $tmargin = $this->pdf->GetY();
@@ -423,10 +433,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['authentication']->getUser()->ACL()->has_right_on_base($rec->get_base_id(), "nowatermark") if (!$this->app['authentication']->getUser()->ACL()->has_right_on_base($rec->get_base_id(), "nowatermark")
@@ -434,19 +448,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) {