SSH This
SSH This

Reputation: 1929

CropMarks in TCPDF

I'm trying to make cropMark's in my PDF, I've seen the example doc:

http://www.tcpdf.org/examples/example_056.phps

And I've downloaded that code and managed to get those cropMarks to work, however I can't seem to add crop marks to my other PDF code. What are the previous requirements for the PDF in order for the cropMark method to work?

I'm making the cropMark like this (50mm right, 50mm down from 0,0):

$pdf->cropMark(50,50, 10, 10, 'TL', array(255,0,0));

I have tried adding this code before I add the content and after. The content is a combination of Cell, writeHTMLCell and SVG content.

I add the page like this:

$pdf->AddPage('L', $page_format, true, false);

I set the margins of the page

$pdf->SetMargins(0,0,0);

And I don't set any Header or Footer margins. What am I doing wrong?

UPDATE: I should mention that I don't get any errors, I just don't see any cropmarks, my background is blue, and (I believe) the crop marks should be red. Also I've tried Adding a new page (AddPage()) and tried drawing crop marks on the new page, but still no luck. Up until now I've found the docs to useful and consistent, so I must be doing something wrong.

Upvotes: 1

Views: 2398

Answers (3)

Alexander Woerl
Alexander Woerl

Reputation: 21

I had an incredible hard time with finding out, how the coordinates work. The following method worked out for me finally and draws the marks and bleeds for any tcpdf document:

/**
 * Enlarges the MediaBox by the slug of the document in all directions and draws cropmarks,
 * registrations marks and color bars
 *
 * @param $tcpdf the internal tcpdf object
 *
 * @access public
 */
public function drawCropbox($tcpdf, $slug = 6)
{
  for ($i = 1; $i <= $tcpdf->getNumPages(); $i++) {
    $tcpdf->setPage($i);
    $width = $tcpdf->getPageWidth();
    $height = $tcpdf->getPageHeight();
    $outerWidth = $width + 2 * $slug;
    $outerHeight = $height + 2 * $slug;
    $barHeight = min($slug - 1, 6);
    $barWidth = min(9 * $barHeight, ($width - $barHeight * 4)/ 2);
    $barHeight = max(1, $barWidth / 9);
    $registrationHeight  = $barHeight / 2;

    $tcpdf->setPageFormat(
      array(
        $outerWidth,
        $outerHeight,
        'Rotate'   => 0,
        'MediaBox' => array(
          'llx' => -$slug, 'lly' => $height + $slug, 'urx' => $width + $slug, 'ury' => -$slug
        ),
      )
    );

    //Crop left top
    $tcpdf->cropMark(
      $x = 0,
      $y = $outerWidth - $height,
      $w = $slug,
      $h = $slug,
      $type = 'A',
      $color = array(0, 0, 0)
    );

    //Crop right top
    $tcpdf->cropMark(
      $x = $width,
      $y = $outerWidth - $height,
      $w = $slug,
      $h = $slug,
      $type = 'B',
      $color = array(0, 0, 0)
    );

    //Crop left bottom
    $tcpdf->cropMark(
      $x = 0,
      $y = $outerWidth,
      $w = $slug,
      $h = $slug,
      $type = 'C',
      $color = array(0, 0, 0)
    );

    //Crop right bottom
    $tcpdf->cropMark(
      $x = $width,
      $y = $outerWidth,
      $w = $slug,
      $h = $slug,
      $type = 'D',
      $color = array(0, 0, 0)
    );

    //Registration left
    $tcpdf->registrationMark(
      $x = -$slug / 2,
      $y = $width - $height / 2 + 2 * $slug,
      $registrationHeight,
      FALSE,
      array(0, 0, 0),
      array(255, 255, 255)
    );

    //Registration top
    $tcpdf->registrationMark(
      $x = $width / 2,
      $y = $outerWidth - $height - $slug / 2,
      $registrationHeight,
      FALSE,
      array(0, 0, 0),
      array(255, 255, 255)
    );

    //Registration right
    $tcpdf->registrationMark(
      $x = $width + $slug / 2,
      $y = $width - $height / 2 + 2 * $slug,
      $registrationHeight,
      FALSE,
      array(0, 0, 0),
      array(255, 255, 255)
    );

    //Registration bottom
    $tcpdf->registrationMark(
      $x = $width / 2,
      $y = $outerWidth + $slug / 2,
      $registrationHeight,
      FALSE,
      array(0, 0, 0),
      array(255, 255, 255)
    );

    //Color Registration Bar
    $tcpdf->colorRegistrationBar(
      $x = $width - $barWidth - $barHeight,
      $y = $outerWidth - $outerHeight + $slug,
      $w = $barWidth,
      $h = $barHeight,
      FALSE,
      TRUE,
      'A,W,R,G,B,C,M,Y,K'
    );

    //Gray Registration Bar
    $tcpdf->colorRegistrationBar(
      $x = $barHeight,
      $y = $outerWidth - $outerHeight + $slug,
      $w = $barWidth,
      $h = $barHeight,
      TRUE,
      FALSE,
      'A'
    );
  }
}

Upvotes: 2

SSH This
SSH This

Reputation: 1929

Unfortunately, I couldn't find out why the crop marks weren't working, but I got around it by drawing them my self using the Line method. Here's the code:

// set the crop marks to be same color as the text, so that they always show up
$pdf->SetLineStyle(array('width' => 0.25, 'color' => $myRGBColor));

// set quarter of an inch and 3/16 of an inch to mm
$qmm = 6.35;
$smQmm = 4.7625;
$cw = $pdf->getPageWidth();
$ch = $pdf->getPageHeight();

// Top left
$pdf->Line($qmm, 0, $qmm, $smQmm);
$pdf->Line(0, $qmm, $smQmm, $qmm);

// Top right
$pdf->Line($cw - $qmm, 0, $cw - $qmm, $smQmm);
$pdf->Line($cw, $qmm, $cw - $smQmm, $qmm);

// Bottom right
$pdf->Line($cw, $ch - $qmm, $cw - $smQmm, $ch - $qmm);
$pdf->Line($cw - $qmm, $ch, $cw - $qmm, $ch - $smQmm);

// Bottom left
$pdf->Line(0, $ch - $qmm, $smQmm, $ch - $qmm);
$pdf->Line($qmm, $ch, $qmm, $ch - $smQmm);

Upvotes: 1

James Mitch
James Mitch

Reputation: 549

If using linux, this shell script adds crop marks (aka registrationmarks) to a pdf.

#!/bin/bash
# takes two arguments, both pdf filenames.
# add cropmarks to  pdf file given as first argument, and writes result to pdf file, given as second argument.
# uses: pdfinfo, ps2pdf, pdftk
# koen 2013

# get bounding box
BOX=`pdfinfo -box $1 | grep 'MediaBox' | head -1`
LEFT=`echo $BOX | awk '{print $2}'`
BOTTOM=`echo $BOX | awk '{print $3}'`
RIGHT=`echo $BOX | awk '{print $4}'`
TOP=`echo $BOX | awk '{print $5}'`
WIDTH=`echo $BOX | awk '{print $4-$2}'`
HEIGHT=`echo $BOX | awk '{print $5-$3}'`

# add postscript code for crop marks
cat >> cropmarks.ps <<EOD
% length of crop mark, in inches
/Crop .5 def 
% length of crop mark, in points
/CropLen Crop 72 mul def 

0 setlinewidth 
$LEFT $BOTTOM moveto CropLen 0 rmoveto 0 CropLen rlineto stroke
$LEFT $BOTTOM moveto 0 CropLen rmoveto CropLen 0 rlineto stroke
$LEFT $TOP moveto CropLen 0 rmoveto 0 CropLen neg rlineto stroke
$LEFT $TOP moveto 0 CropLen neg rmoveto CropLen 0 rlineto stroke
$RIGHT $BOTTOM moveto CropLen neg 0 rmoveto 0 CropLen rlineto stroke
$RIGHT $BOTTOM moveto 0 CropLen rmoveto CropLen neg 0 rlineto stroke
$RIGHT $TOP moveto CropLen neg 0 rmoveto 0 CropLen neg rlineto stroke
$RIGHT $TOP moveto 0 CropLen neg rmoveto CropLen neg 0 rlineto stroke

showpage
EOD
ps2pdf -dDEVICEWIDTHPOINTS=$WIDTH -dDEVICEHEIGHTPOINTS=$HEIGHT cropmarks.ps cropmarks.pdf
pdftk $1 stamp cropmarks.pdf output $2
rm -f cropmarks.ps cropmarks.pdf
#not truncated

Upvotes: 1

Related Questions