Fix PDF generation for print

This commit is contained in:
Nicolas Le Goff
2014-06-16 20:27:54 +02:00
parent 680e57d0c8
commit 2328bc772d
3 changed files with 60 additions and 42 deletions

View File

@@ -35,17 +35,14 @@ class Printer implements ControllerProviderInterface
$controllers->post('/print.pdf', function (Application $app) {
$printer = new RecordHelper\Printer($app, $app['request']);
$request = $app['request'];
$layout = $request->request->get('lay');
$layout = $app['request']->request->get('lay');
foreach ($printer->get_elements() as $record) {
$app['phraseanet.logger']($record->get_databox())
->log($record, \Session_Logger::EVENT_PRINT, $layout, '');
$app['phraseanet.logger']($record->get_databox())->log($record, \Session_Logger::EVENT_PRINT, $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->setMaxAge(0);

View File

@@ -31,7 +31,6 @@ class Printer extends RecordHelper
parent::__construct($app, $Request);
$grep = function (\record_adapter $record) {
try {
return $record->get_thumbnail()->get_type() == \media_subdef::TYPE_IMAGE ||
$record->get_preview()->get_type() == \media_subdef::TYPE_IMAGE;

View File

@@ -42,18 +42,18 @@ class PDF
case self::LAYOUT_PREVIEWCAPTIONTDM:
try {
$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()) {
continue 2;
}
if ($subdef->get_type() !== \media_subdef::TYPE_IMAGE)
continue 2;
$subdef = $record->get_subdef('thumbnail');
if (!$subdef->is_physically_present())
continue 2;
if ($subdef->get_type() !== \media_subdef::TYPE_IMAGE)
if ($subdef->get_type() !== \media_subdef::TYPE_IMAGE) {
continue 2;
}
} catch (\Exception $e) {
continue 2;
}
@@ -61,12 +61,14 @@ class PDF
case self::LAYOUT_THUMBNAILLIST:
case self::LAYOUT_THUMBNAILGRID:
try {
$subdef = $record->get_subdef('thumbnail');
if (!$subdef->is_physically_present())
$subdef = $record->get_thumbnail();
if (!$subdef->is_physically_present()) {
continue 2;
}
if ($subdef->get_type() !== \media_subdef::TYPE_IMAGE)
throw new \Exception('Not suitable');
if ($subdef->get_type() !== \media_subdef::TYPE_IMAGE) {
continue 2;
}
} catch (\Exception $e) {
continue 2;
}
@@ -158,6 +160,10 @@ class PDF
$subdef = $rec->get_subdef('preview');
if ($subdef->get_type() !== \media_subdef::TYPE_IMAGE) {
$subdef = $rec->get_thumbnail();
}
$fimg = $subdef->get_pathfile();
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');
$fimg = $subdef->get_pathfile();
$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())
$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))
if ($this->pdf->GetY() > $this->pdf->getPageHeight() - (6 + $finalheight + 20))
$this->pdf->AddPage();
$title = "record : " . $rec->get_title();
@@ -270,8 +283,8 @@ class PDF
if ($fimg) {
$y = $this->pdf->GetY();
$this->pdf->Image($fimg, $lmargin, $y, $wimg, $himg);
$this->pdf->SetY($y);
$this->pdf->Image($fimg, $lmargin, $y, $finalWidth, $finalheight);
$this->pdf->SetY($y + 3);
}
$nf = 0;
@@ -290,8 +303,8 @@ class PDF
$this->pdf->Write(6, "\n");
$nf++;
}
if ($this->pdf->PageNo() == $p0 && ($this->pdf->GetY() - $y0) < $himg)
$this->pdf->SetY($y0 + $himg);
if ($this->pdf->PageNo() == $p0 && ($this->pdf->GetY() - $y0) < $finalheight)
$this->pdf->SetY($y0 + $finalheight);
$ndoc++;
}
$this->pdf->SetLeftMargin($lmargin);
@@ -309,10 +322,7 @@ class PDF
$this->pdf->AddPage();
if ($withtdm === "CALCPAGES") {
if ($presentationpage)
$rec->set_number($this->pdf->PageNo() + 1);
else
$rec->set_number($this->pdf->PageNo());
$rec->set_number($this->pdf->PageNo());
}
$lmargin = $this->pdf->GetX();
$himg = 0;
@@ -413,10 +423,14 @@ class PDF
}
}
$y = $this->pdf->GetY() + 3;
$y = $this->pdf->GetY() + 5;
$subdef = $rec->get_subdef('preview');
if ($subdef->get_type() !== \media_subdef::TYPE_IMAGE) {
$subdef = $rec->get_thumbnail();
}
$f = $subdef->get_pathfile();
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);
$wimg = $himg = 150; // preview dans un carre de 150 mm
if ($subdef->get_width() > 0 && $subdef->get_height() > 0) {
if ($subdef->get_width() > $subdef->get_height())
$himg = $wimg * $subdef->get_height() / $subdef->get_width();
else
$wimg = $himg * $subdef->get_width() / $subdef->get_height();
// 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;
}
}
$this->pdf->Image($f, $lmargin, $y, $wimg, $himg);
$this->pdf->Image($f, (210 - $finalWidth) / 2, $y, $finalWidth, $finalheight);
if ($miniConv != NULL) {
foreach ($miniConv as $oneF)
unlink($oneF);
}
$this->pdf->SetXY($lmargin, $y += ( $himg + 5));
$this->pdf->SetXY($lmargin, $y += ( $finalheight + 5));
$nf = 0;
if ($write_caption) {