为现有PDF添加边距和裁剪标记

时间:2014-02-17 13:38:20

标签: php pdf imagemagick ghostscript

为了准备PDF文件进行打印,我正在寻找一种命令行/编程方式来修改现有的PDF文件并执行以下操作: - 为每页的每一边添加一个3毫米的白色边距(因此新PDF的宽度将增加6毫米,高度将增加6毫米) - 在每页上添加裁剪标记

以下是我尝试过的一些命令:

我首先尝试添加BleedBox,但由于没有调整pdf的大小,因此没有达到预期的效果:

gs -sDEVICE=pdfwrite -o out.pdf -c "[/BleedBox[54 54 1314 810] /PAGES pdfmark" -f in.pdf

以下ghostscript命令放大pdf并在每个页面的顶部和右侧添加白色边距,但内容不居中:

gs -sDEVICE=pdfwrite -o out.pdf -r300x300 -g3000x3000 -f in.pdf

我还尝试使用imagemagick调整pdf大小,但以下命令也缩放了pdf的内容:

convert -density 300 -colorspace RGB -quality 100 -border 200x200 in.pdf out.pdf

到目前为止,我还没有找到任何方法来添加裁剪标记。

有人可以帮我解决边距和裁剪标记吗?

提前致谢!

亲切的问候, 迈克尔

2 个答案:

答案 0 :(得分:1)

迈克尔,

检查http://www.setasign.com/products/fpdi/about

请参阅以下代码段,使用该库向文档中添加3毫米。您还可以使用相同的库添加图像(cropigns)。你仍然需要找到如何将它们放在角落里......

w ^

require_once('library/fpdf.php');
require_once('library/fpdi.php');

$name="cropsign.png";
$im = imagecreatefrompng($name);
imagefilter($im, IMG_FILTER_COLORIZE, 0, 255, 0);
imagepng($im,$name);

$pdf = new FPDI('P','mm',array(213,300));// original 210/297 for A4 document
if (file_exists("./".$file)){
    $pagecount = $pdf->setSourceFile($file);
} else {
    echo 'Fail';
    return FALSE;
}
 for($i=1; $i <= $pagecount; $i++) {
    $tpl = $pdf->importPage($i);
    $pdf->addPage();
    $pdf->useTemplate($tpl, 1, 1, 0, 0, TRUE);
    $pdf->Image($name, $xxx, $yyy, 0, 0, 'png');
    $pdf->Image($name, $xxx, $yyy, 0, 0, 'png');
    $pdf->Image($name, $xxx, $yyy, 0, 0, 'png');
    $pdf->Image($name, $xxx, $yyy, 0, 0, 'png');
}
if ($outdir === TRUE){
    return $pdf->Output();
} else {
    return $pdf;
}

答案 1 :(得分:1)

基于上面的FPDF / FPDI代码示例,我设法添加了出血和裁剪标记。 唯一的区别是,我将裁剪标记绘制为线条而不是将图像放在角落中。

对于那些想要这样做的人,下面是将流失和裁剪标记添加到现有pdf的代码:

    $bleedInMM = 3; // the bleed in mm on each side
    $pdfWidthInMM = $this->getPdfWidthInMM();
    $pdfHeightInMM = $this->getPdfHeightInMM();

    //width and height of new pdf. the value of $bleedInMM is doubled to have the bleed on both sides of the page
    $newWidth = ($pdfWidthInMM + ($bleedInMM * 2); 
    $newHeight = ($pdfWidthInMM + ($bleedInMM * 2);

    $pdf = new \fpdi\FPDI(
            $pdfWidthInMM > $pdfWidthInMM ? 'L' : 'P', // landscape or portrait?
            'mm',
            array(
                $newWidth, 
                $newHeight
            ));

    if (file_exists($srcPdfFilePath)){ 
         $pagecount = $pdf->setSourceFile($srcPdfFilePath); 
    } else { 
        error_log("Error! file: ".$srcPdfFilePath." does not exist");
        return FALSE; 
    } 

    // make the crop line a little shorter so they don't touch each other
    $cropLineLength = $bleedInMM - 1;

     for($i=1; $i <= $pagecount; $i++) { 
         $tpl = $pdf->importPage($i); 
         $pdf->addPage(); 
         $size = $pdf->getTemplateSize($tpl);

         $pdf->useTemplate($tpl, $bleedInMM, $bleedInMM, 0, 0, TRUE); 

         $pdf->SetLineWidth(0.25);

         // top left crop marks
         $pdf->Line($bleedInMM /* x */, 0 /* y */, $bleedInMM /* x */, $cropLineLength /* y */); // horizontal top left
         $pdf->Line(0 /* x */, $bleedInMM /* y */, $cropLineLength /* x */, $bleedInMM /* y */); // vertical top left

         // top right crop marks
         $pdf->Line($newWidth - $bleedInMM /* x */, 0 /* y */, $newWidth - $bleedInMM /* x */, $cropLineLength /* y */); // horizontal top right
         $pdf->Line($newWidth - $cropLineLength /* x */, $bleedInMM /* y */, $newWidth /* x */, $bleedInMM /* y */); // vertical top right

         // bottom left crop marks
         $pdf->Line(0 /* x */, $newHeight - $bleedInMM /* y */, $cropLineLength /* x */, $newHeight - $bleedInMM /* y */); // horizontal bottom left
         $pdf->Line($bleedInMM /* x */, $newHeight - $cropLineLength /* y */, $bleedInMM /* x */, $newHeight /* y */); // vertical bottom left

         // bottom right crop marks
         $pdf->Line($newWidth - $cropLineLength /* x */, $newHeight - $bleedInMM /* y */, $newWidth /* x */, $newHeight - $bleedInMM /* y */); // horizontal top right
         $pdf->Line($newWidth - $bleedInMM /* x */, $newHeight - $cropLineLength /* y */, $newWidth - $bleedInMM /* x */, $newHeight /* y */); // vertical top right
     }

     return $pdf->Output($destinationPdfFilePath,'F');
相关问题