Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将边距和裁剪标记添加到现有PDF_Php_Pdf_Imagemagick_Ghostscript - Fatal编程技术网

Php 将边距和裁剪标记添加到现有PDF

Php 将边距和裁剪标记添加到现有PDF,php,pdf,imagemagick,ghostscript,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 以下gh

为了准备要打印的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
到目前为止,我还没有找到任何添加裁剪标记的方法

谁能帮我弄一下边距和作物标记吗

提前谢谢

亲切问候,, 迈克尔

迈克尔

检查

请参阅以下代码段,使用库向文档中添加3mm。您还可以使用相同的库添加图像(cropsigns)。但是你仍然需要知道如何把它们放在角落里

韩元

W

require_once('library/fpdf.php');
一次需要_('library/fpdi.php');
$name=“cropsign.png”;
$im=imagecreatefrompng($name);
imagefilter($im,IMG\u FILTER\u COLORIZE,0,255,0);
imagepng($im,$name);
$pdf=新的FPDI('P','mm',数组(213300));//A4文件原件210/297
如果(文件_存在(“./”$file)){
$pagecount=$pdf->setSourceFile($file);
}否则{
回声“失败”;
返回FALSE;
}
对于($i=1;$i进口页($i);
$pdf->addPage();
$pdf->useTemplate($tpl,1,1,0,0,TRUE);
$pdf->Image($name,$xxx,$yyyy,0,0,'png');
$pdf->Image($name,$xxx,$yyyy,0,0,'png');
$pdf->Image($name,$xxx,$yyyy,0,0,'png');
$pdf->Image($name,$xxx,$yyyy,0,0,'png');
}
如果($outdir==TRUE){
返回$pdf->Output();
}否则{
返回$pdf;
}

基于上述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');
$bleedInMM=3;//每侧的出血量以mm为单位
$pdfWidthInMM=$this->getPdfWidthInMM();
$pdfHeightInMM=$this->getPdfHeightInMM();
//新pdf的宽度和高度。$bleedInMM的值是原来的两倍,以使页面两侧都有出血
$newWidth=($pdfWidthInMM+($bleedInMM*2);
$newHeight=($pdfWidthInMM+($bleedInMM*2);
$pdf=new\fpdi\fpdi(
$pdfWidthInMM>$pdfWidthInMM?'L':'P',//横向还是纵向?
嗯,,
排列(
$newWidth,
$newHeight
));
如果(文件_存在($srcpdfilepath)){
$pagecount=$pdf->setSourceFile($srcpdfilePath);
}否则{
错误日志(“错误!文件:“.$SRCPDFILEPATH.”不存在);
返回FALSE;
} 
//将作物线缩短一点,这样它们就不会相互接触
$cropLineLength=$bleedInMM-1;
对于($i=1;$i进口页($i);
$pdf->addPage();
$size=$pdf->getTemplateSize($tpl);
$pdf->useTemplate($tpl、$bleedInMM、$bleedInMM,0,0,TRUE);
$pdf->SetLineWidth(0.25);
//左上裁剪标记
$pdf->Line($bleedInMM/*x*/,0/*y*/,$bleedInMM/*x*/,$cropLineLength/*y*/);//水平左上角
$pdf->Line(0/*x*/,$bleedInMM/*y*/,$cropLineLength/*x*/,$bleedInMM/*y*/);//垂直左上角
//右上裁剪标记
$pdf->Line($newWidth-$bleedInMM/*x*/,0/*y*/,$newWidth-$bleedInMM/*x*/,$cropLineLength/*y*/);//水平右上角
$pdf->Line($newWidth-$cropLineLength/*x*/,$bleedInMM/*y*/,$newWidth/*x*/,$bleedInMM/*y*/);//垂直右上角
//左下裁剪标记
$pdf->Line(0/*x*/,$newHeight-$bleedInMM/*y*/,$cropLineLength/*x*/,$newHeight-$bleedInMM/*y*/);//水平左下角
$pdf->Line($bleedInMM/*x*/,$newHeight-$cropLineLength/*y*/,$bleedInMM/*x*/,$newHeight/*y*/);//垂直左下角
//右下裁剪标记
$pdf->Line($newWidth-$cropLineLength/*x*/,$newHeight-$bleedInMM/*y*/,$newWidth/*x*/,$newHeight-$bleedInMM/*y*/);//水平右上角
$pdf->Line($newWidth-$bleedInMM/*x*/,$newHeight-$cropLineLength/*y*/,$newWidth-$bleedInMM/*x*/,$newHeight/*y*/);//垂直右上角
}
返回$pdf->Output($destinationPdfFilePath,'F');

您能告诉我们您在ghostscript&Imagemagick方面做了哪些尝试吗?因为:要求我们推荐或查找工具、库或喜爱的非现场资源的问题对于堆栈溢出来说是离题的,因为它们往往会吸引自以为是的答案和垃圾邮件。相反,请描述问题以及迄今为止为解决该问题所做的工作。好的,我更新了m我用我试验过的命令来回答这个问题。
    $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');