给现有的PDF添加页边距和裁剪标记

3
为了准备将PDF文件用于打印,我正在寻找一种命令行/编程方式来修改现有的PDF文件并执行以下操作: - 在每个页面的每侧添加一个白色的3mm边距(因此新的PDF在宽度和高度上将增加6mm) - 在每个页面上添加裁剪标记
这里是我尝试过的几个命令:
我首先尝试添加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

到目前为止,我还没有找到任何添加裁切标记的方法。 请问有谁能帮忙解决边距和裁切标记的问题吗? 提前致谢! 此致, 迈克尔

请问您能否展示一下您使用Ghostscript和Imagemagick尝试过的内容吗?因为:在Stack Overflow上,询问我们推荐或寻找工具、库或喜爱的外部资源的问题是不合适的,因为它们往往会吸引带有个人观点的答案和垃圾信息。相反,请描述问题以及迄今为止已经采取的解决方案。 - Ocaso Protal
好的,我更新了我的问题,并添加了我尝试过的命令。 - Michael
2个回答

3

根据上述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');

1

Michael,

请查看http://www.setasign.com/products/fpdi/about

以下是使用该库将3mm添加到文档的片段。您也可以使用同一库添加图像(裁剪标志)。但是,您仍需要找出如何将它们放置在角落中...

Kr

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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接