【问题标题】:Ghostscript rotate pagesGhostscript 旋转页面
【发布时间】:2012-05-14 11:42:41
【问题描述】:

我使用 Ghostscript 将 PDF 文档转换为 PCL 进行打印。最近我有一个额外的要求,即在打印之前必须将所有页面旋转到纵向。我找到了一种使用带有以下命令和 postscript 功能的 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 文件。我试图修改这个功能,用他们的纵向对应物替换横向尺寸,但还没有能够产生有效的后记。我需要指出正确的方向才能生成与以下伪代码等效的 postscript。

for(each page)
{
   if(PageSize == [792 612])
       PageSize = [612 792];
}

我知道有非 Ghostscript 方式来旋转页面,但如果我能做到这一点,它会很好地融入我的流程并且不会降低性能。

这是我的一个 pdf 文件的示例: Sample1.pdf

【问题讨论】:

    标签: pdf ghostscript postscript printer-control-language


    【解决方案1】:

    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 一起使用:

    gs ...... prolog.ps job.ps

    我已经尝试过了,但没有使用横向文件,因为我手头没有。另请注意,可以构建一个 PostScript 程序来解决这个问题。

    【讨论】:

    【解决方案2】:

    我找到了一个可行的解决方案。它不像我希望的那样通用,但它满足了我的所有要求。

    以下 postscript 脚本会将 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
    

    此 postscript 脚本会将 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 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 下获得许可。

    【讨论】:

      【解决方案3】:

      虽然Zig158的回答效果很好,但从那以后又出现了一个新选项 -d固定媒体 女巫适用于任何纸张尺寸,不仅适用于 a4。

      更多详情请参阅Ghostscript bug tracker

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-22
        • 2011-03-09
        相关资源
        最近更新 更多