Skip to content

Commit

Permalink
Added "Core > Canvas > Add crop marks" demo
Browse files Browse the repository at this point in the history
  • Loading branch information
JanSlabon committed Jul 22, 2024
1 parent 668bf17 commit 6682d2e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
Binary file added assets/pdfs/camtown/Business-Card-filled.pdf
Binary file not shown.
8 changes: 8 additions & 0 deletions public/demos/1-Core/canvas/add-crop-marks/demo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "Add Crop Marks",
"teaserText": "Update the page boundaries and add some crop marks to an existing PDF page.",
"requires": [
"SetaPDF-Core"
],
"faIcon": ""
}
66 changes: 66 additions & 0 deletions public/demos/1-Core/canvas/add-crop-marks/script.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

// load and register the autoload function
require_once '../../../../../bootstrap.php';

// create a writer instance
$writer = new \SetaPDF_Core_Writer_Http('with-crop-marks.pdf', true);

// create a document instance
$document = SetaPDF_Core_Document::loadByFilename($assetsDirectory . '/pdfs/camtown/Business-Card-filled.pdf', $writer);

$pages = $document->getCatalog()->getPages();

$mm = 2;
$points = $mm * 2.8346456693; // 2mm to Points

// get the page object
$page = $pages->getPage(1);

// let's save the main TrimBox
$trimBox = $page->getTrimBox();

// get the MediaBox and enlarge it
$box = $page->getMediaBox();
$box->setLlx($box->getLlx() - $points);
$box->setLly($box->getLly() - $points);
$box->setUrx($box->getUrx() + $points);
$box->setUry($box->getUry() + $points);

// set the new box as both Media- and CropBox
$page->setMediaBox($box);
$page->setCropBox($box);

// now set a reduced TrimBox
$trimBox->setLlx($trimBox->getLlx() + $points);
$trimBox->setLly($trimBox->getLly() + $points);
$trimBox->setUrx($trimBox->getUrx() - $points);
$trimBox->setUry($trimBox->getUry() - $points);
$page->setTrimBox($trimBox);

// make sure we have a clean graphic state
$page->getStreamProxy()->encapsulateExistingContentInGraphicState();
// get the canvas...
$canvas = $page->getCanvas();

// and draw the crop marks - we use values of both boundary boxes
$canvas->saveGraphicState()
->path()
->setLineWidth(.1)
->setStrokingColor(.3)
->draw()
// left-top
->line($box->getLlx(), $trimBox->getUry(), $trimBox->getLlx() - $points / 2, $trimBox->getUry())
->line($trimBox->getLlx(), $box->getUry(), $trimBox->getLlx(), $trimBox->getUry() + $points / 2)
// right-top
->line($trimBox->getUrx(), $box->getUry(), $trimBox->getUrx(), $trimBox->getUry() + $points / 2)
->line($trimBox->getUrx() + $points / 2, $trimBox->getUry(), $box->getUrx(), $trimBox->getUry())
// right-bottom
->line($box->getUrx(), $trimBox->getLly(), $trimBox->getUrx() + $points / 2, $trimBox->getLly())
->line($trimBox->getUrx(), $box->getLly(), $trimBox->getUrx(), $trimBox->getLly() - $points / 2)
// left-bottom
->line($trimBox->getLlx(), $box->getLly(), $trimBox->getLlx(), $trimBox->getLly() - $points / 2)
->line($box->getLlx(), $trimBox->getLly(), $trimBox->getLlx() - $points / 2, $trimBox->getLly())
->restoreGraphicState();

$document->save()->finish();

0 comments on commit 6682d2e

Please sign in to comment.