【问题标题】:CropMarks in TCPDFTCPDF 中的裁剪标记
【发布时间】:2013-01-11 19:44:14
【问题描述】:

我正在尝试在我的 PDF 中制作cropMark,我已经看过示例文档:

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

我已经下载了该代码并设法让这些cropMarks 工作,但是我似乎无法将裁剪标记添加到我的其他PDF 代码中。为了使cropMark 方法起作用,对PDF 的先前要求是什么?

我正在制作这样的cropMark(向右50mm,从0,0向下50mm):

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

我尝试在添加内容之前和之后添加此代码。内容是 Cell、writeHTMLCell 和 SVG 内容的组合。

我这样添加页面:

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

我设置了页面的边距

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

而且我没有设置任何页眉或页脚边距。我做错了什么?

更新:我应该提到我没有收到任何错误,我只是没有看到任何裁剪标记,我的背景是蓝色的,并且(我相信)裁剪标记应该是红色的。我也尝试过添加新页面(AddPage())并尝试在新页面上绘制裁剪标记,但仍然没有运气。到目前为止,我发现这些文档有用且一致,所以我一定做错了什么。

【问题讨论】:

    标签: php tcpdf


    【解决方案1】:

    我很难弄清楚坐标是如何工作的。以下方法最终为我解决了,并为任何 tcpdf 文档绘制标记和出血:

    /**
     * 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'
        );
      }
    }
    

    【讨论】:

    • 这对我来说是最好的解决方案。但我建议您添加答案,解释如何将其包括在内。我必须将它放在一个单独的文件中(我包含在 require_once() 中,如下所示:class MYPDF extends TCPDF { [... your code went here ...] } 然后我使用:$pdf = new MYPDF(.... 创建了我的对象
    【解决方案2】:

    如果使用 linux,此 shell 脚本会在 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
    

    【讨论】:

    • @James Mitch,我正在尝试这个。但是,它给了我错误pdfinfo: command not found。我正在使用 Cent OS,有什么想法吗?
    • @user961627 你用的是什么版本的 CentOS?试试sudo yum install xpdf
    • Cent OS 6. yum install pdfinfo 时与 poppler-utils 发生冲突。所以我希望 Line 版本会有用。
    【解决方案3】:

    不幸的是,我不知道为什么裁剪标记不起作用,但我通过使用Line 方法自己绘制它们来解决它。代码如下:

    // 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);
    

    【讨论】:

    • 您能否分享一下您是如何使用Line 做到的,我现在也被困在这个问题上。
    猜你喜欢
    • 2013-08-10
    • 2014-10-18
    • 1970-01-01
    • 2019-02-13
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    • 2019-09-21
    相关资源
    最近更新 更多