平铺大型PDF文件并添加页眉和页脚

时间:2017-07-16 11:05:56

标签: linux pdf ghostscript tile pdftk

在Ubuntu 14.04和16.04上运行的Web应用程序使用wkhtmltopdf创建大型自定义页面大小的PDF文件我正在寻找一种解决方案,将大型pdf页面平铺为DIN纸张大小,添加页眉和页脚并加入单个页面分成一个文件。

目前我生成单个文件的步骤正在运行但速度很慢但是这是唯一的运行方式,因为pdfposter只创建了空白页面:

  1. 使用wkhtmltopdf

  2. 创建大型pdf文件
  3. 使用ghostscript创建叠加pdf

    exec('gs -o ' . $path . $randomValue . '.overlay.pdf -sDEVICE=pdfwrite 
    -sFONTPATH=/usr/share/fonts/truetype -g' . $width . 'x' . $height . '
    -c '. '"/OpenSans findfont 6 scalefont setfont ' .
    '30 10 moveto (' . $footerLeft . ') show '.
    $center . ' 10 moveto (' . $footerCenter . ') dup stringwidth pop 2 div
    neg 0 rmoveto show '.
    '0 10 moveto (' . $footerRight . ') dup stringwidth pop neg ' .
    $right . ' add 0 rmoveto show ' .
    '30 ' . $top . ' moveto (' . $headerLeft . ') show ' .
    $center . ' ' . $top . ' moveto (' . $headerCenter . ') dup stringwidth
    pop 2 div neg 0 rmoveto show ' .
    '0 ' . $top . ' moveto (' . $headerRight . ') dup stringwidth pop
    neg ' . $right . ' add 0 rmoveto show"');
    
    e.g.
    gs -o overlay -sDEVICE=pdfwrite \
    -sFONTPATH=/usr/share/fonts/truetype -g5950x8420 \
    -c "/OpenSans findfont 6 scalefont setfont \
    30 10 moveto (' . $footerLeft . ') show"
    
  4. 将步骤1中创建的大型pdf文件平铺为几个带有ghostscript的单个pdf文件,宽度和高度比输出文件的纸张大小小60个

    exec('gs -o ' . $path . $j . '.' . $i . '.' . $splitFile . '
    -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -g' . $width . 'x' .
    $height . ' -c "<</PageOffset [-' . $widthOffset . ' ' .
    $heightOffset . ']>>
    setpagedevice" -f ' . $pdfFile);
    
    e.g.
    gs -o outputfile.pdf -sDEVICE=pdfwrite -dPDFSETTINGS=/depress \
    -g5890x8360 -c"<</PageOffset [0 0]>> setpagedevice" -f inputfile.pdf
    
  5. 4根据DIN纸张尺寸将新纸张尺寸设置为单个pdf页

        exec('gs -o ' . $path . '1.' . $j . '.' . $i . '.' . $splitFile . '
        -sDEVICE=pdfwrite -g' . $width . 'x' . $height . '
        -c "<</PageOffset [30 30]>> setpagedevice" -f ' . $path . $j . '.' .
        $i . '.' . $splitFile);
    
        e.g.
        gs -o outputfile.pdf -sDEVICE=pdfwrite -g5950x8420 \
        -c "<</PageOffset [30 30]>> setpagedevice" -f inputfile.pdf
    
    1. 使用pdftk

      将步骤2中创建的叠加层添加到所有单个pdf页面
      exec('pdftk '. $path . '1.' . $j . '.' . $i . '.' . $splitFile . '
      stamp ' . $path . $randomValue . '.overlay.pdf output ' . $path . '2.'
      . $j . '.' . $i . '.' . $splitFile);
      
    2. 使用pdftk将所有单个pdf文件作为页面加入一个pdf文件

    3. 特别是对于使用wkhtmltopdf(例如高达20,000pts)创建的非常大的页面,如果所选输出格式为A4,则需要很长时间,因为在步骤3中创建了大量页面。

      有没有人知道如何通过创建已经具有30pts(顶部,左侧,右侧,底部)空白边框的pdf文件来跳过步骤4?如果我也可以通过在步骤3(平铺)中打印页眉和页脚来摆脱步骤5,那么在创建文档时我将节省大量时间。

      我已经尝试在分割时直接在页面上打印页眉和页脚,但是只要我合并命令,文本就会变白或不可见。

          gs -o test2.pdf -sDEVICE=pdfwrite -sFONTPATH=/usr/share/fonts/truetype \
          -dPDFSETTINGS=/prepress -g5950x8420 -c "<</PageOffset [0 0] >> \
          setpagedevice 255 0 0 setrgbcolor /OpenSans findfont 64 scalefont \
          setfont 100 100 moveto (footerline) show " -f J17ggfsh9W.data.pdf
      

      我很高兴能得到一切帮助!

      安迪

0 个答案:

没有答案