Ghostscript 旋转页面

6

我使用Ghostscript将PDF文档转换为PCL以进行打印。最近,我有额外的要求,即在打印之前必须将所有页面旋转为纵向。我已经找到了一种使用Ghostscript和后置脚本函数来实现这个要求的方法,如下所示:

"C:\Program Files (x86)\gs\bin\gswin32c.exe" "-dNOPAUSE" "-dNOPROMPT" "-dBATCH" "-sDEVICE=pxlmono" "-Ic:\Program Files (x86)\gs\fonts\;c:\Program Files (x86)\gs\lib\;c:\Program Files (x86)\gs\lib\;" "-r300" "-sOutputFile=C:\EXPORTFILE_e542e04f-5e84-4c8e-9b41-55480cd5ec52.cache" "rotate612x792.ps" "C:\EXPORTFILE_3a5de9da-d9ca-4562-8cb6-10fb8715385a.cache"

rotate612x792.ps的内容

%! Rotate Pages
<< /Policies << /PageSize 5 >> 
   /PageSize [612 792] 
   /InputAttributes currentpagedevice 
   /InputAttributes get mark exch {1 index /Priority eq not {pop << /PageSize [612 792] >>} if }  forall >>
   >> setpagedevice

问题在于该函数将所有页面大小都替换为信纸大小。我的文档有时是法律或A4大小的。我已经尝试修改此函数以将横向大小替换为其纵向对应物,但无法生成可用的后置脚本。我需要指点方向,以生成以下伪代码的后置脚本等价物。
for(each page)
{
   if(PageSize == [792 612])
       PageSize = [612 792];
}

我知道有非Ghostscript的方法来旋转页面,但如果我能让它工作,它会很好地适应我的流程,并且不会降低性能。
这是我的一个PDF文件的样本: Sample1.pdf
3个回答

4
PostScript是一种编程语言,因此您可以在其中执行许多操作。你需要重新定义请求页面大小的操作。在PostScript中,页面大小和内容是分开的,因此需要执行两个操作:
1)将媒体请求从横向转换为纵向
2)旋转页面内容
最简单的方法是重新定义“setpagedevice”运算符。以下是一个示例:
/oldsetpagedevice /setpagedevice load def %% copy original definition

/setpagedevice {
  dup /PageSize known {                   %% Do we have a page size redefinition ?
    dup /PageSize get                     %% get the array if so
    aload pop                             %% get elements remove array copy
    gt {                                  %% is width > height ?
      dup /PageSize get aload             %% get size array, put content on stack
      3 1 roll                            %% roll stack to put array at back
      exch                                %% swap width and height
      3 -1 roll                           %% bring array back to front of stack
      astore                              %% put swapped elements into array
      /PageSize exch                      %% put key on stack and swap with value
      2 index                             %% copy the original dict
      3 1 roll                            %% move dict to back of stack
      put                                 %% put new page size array in dict
      90 rotate                           %% rotate content 90 degrees anti-clockwise
    } if
  } if
  oldsetpagedevice                        %% call the original definition
} bind def

该功能检查配置更改是否导致页面大小的变化,如果是,则获取新的大小并查看宽度是否大于高度(横向页面的简单定义)。如果为真,则通过交换宽度和高度来修改请求,然后将页面内容旋转90度。您可以通过将上述内容放入文件中(例如prolog.ps),然后在自己的作业之前运行该文件,与Ghostscript一起使用。我已经尝试过这个功能,但没有横向文档可用。请注意,也有可能构建一个PostScript程序来使此功能无效。

我尝试了这个脚本,它没有产生错误,但也没有旋转页面。这是一个输入PDF文件的样本。 https://docs.google.com/open?id=0B2LzHaZ9pUynWXc2X0J3M0RvN28 - Zig158

3
我找到了一个可行的解决方案。它不如我希望的那样灵活,但它满足了我所有的需求。
以下的附言脚本将A4、Letter和Legal文档旋转为纵向。如果要让它处理其他页面大小,请调整最小值和最大值。
%!PS
  %   Sequence to set up for a single page size, auto fit all pages.
  %   Autorotate A4 Letter and Legal page sizes to Portrait
  << /Policies << /PageSize 3 >>
     /InputAttributes currentpagedevice /InputAttributes get    %current dict
     dup { pop 1 index exch undef } forall    % remove all page sizes
     dup 0 << /PageSize [ 595 0 612 99999 ] >> put    % [ min-w min-h max-w max-h ]
  >> setpagedevice

这个后置脚本可以将A4、Letter和Legal文件旋转为横向格式。唯一的区别在于Min/Max页面大小值。

%!PS
  %   Sequence to set up for a single page size, auto fit all pages.
  %   Autorotate A4 Letter and Legal page sizes to Landscape
  << /Policies << /PageSize 3 >>
     /InputAttributes currentpagedevice /InputAttributes get    %current dict
     dup { pop 1 index exch undef } forall    % remove all page sizes
     dup 0 << /PageSize [ 0 595 99999 612 ] >> put    % [ min-w min-h max-w max-h ]
  >> setpagedevice

这个解决方案基于我在hylafax项目的源代码中找到的auto-rotate.ps文件。该项目似乎是根据BSD许可证进行许可的。


1

尽管Zig158的答案效果很好,但此后出现了一个新选项-dFIXEDMEDIA,适用于任何纸张大小,而不仅仅是a4。

有关详细信息,请参见Ghostscript bug跟踪器


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