【问题标题】:Add margin and crop marks to existing PDF向现有 PDF 添加边距和裁剪标记
【发布时间】:2014-03-16 20:06:11
【问题描述】:

为了准备要打印的 PDF 文件,我正在寻找一种命令行/编程方式来修改现有 PDF 文件并执行以下操作: - 在每一页的每一边添加一个白色的 3mm 边距(这样新的 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

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

谁能帮我解决边距和裁剪标记?

提前致谢!

亲切的问候, 迈克尔

【问题讨论】:

  • 你能告诉我们你用ghostscript和Imagemagick尝试了什么吗?因为:要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是无关紧要的,因为它们往往会吸引固执己见的答案和垃圾邮件。相反,请描述问题以及迄今为止为解决该问题所做的工作。
  • 好的,我用我试验过的命令更新了我的问题。

标签: php pdf imagemagick ghostscript


【解决方案1】:

迈克尔,

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

请参阅以下 sn-p 使用库向文档添加 3mm。您还可以使用相同的库添加图像(裁剪标志)。不过,您仍然需要了解如何将它们放置在角落...

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

【讨论】:

    【解决方案2】:

    基于上述 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');
    

    【讨论】:

      猜你喜欢
      • 2013-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      • 1970-01-01
      • 2011-08-13
      相关资源
      最近更新 更多