php - 向现有 PDF 添加边距和裁剪标记

标签 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

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

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

提前致谢!

亲切的问候, 迈克尔

最佳答案

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

对于那些想要这样做的人,这里是将出血和裁剪标记添加到现有 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');

关于php - 向现有 PDF 添加边距和裁剪标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21830581/

相关文章:

pdf - ImageMagick 或 GhostScript : convert a multi-page TIFF to a multi-page PDF

ios - 使用 Swift for iOS 以编程方式创建和存储 PDF 文档

php - 无法在我的服务器中安装 imagemagick

php - 带缓冲区的内联 <video> 大文件

php - CORS/访问控制允许来源

python - 使用 reportlab 从图像创建 A4 大小的 PDF

image-processing - 使用 GraphicsMagick 使用文本/图像应用水印

c++ - Magick++ ErrorMissingDelegate

php - 将大数据存储在 session 变量中是一种好习惯吗?

javascript - 如何在设计模式打开的情况下为父 div 内新创建的 div 提供 id