命令行工具,用于标记PDF以在“单页”视图中打开


Command line tool to mark PDF to open in Single Page view

简短问题:是否有PDF命令行/shell工具(用于osx)可以在PDF上设置必要的属性,以便在"单页视图"(适合窗口)中初始打开


更多信息

我目前正在使用WkhtmlToPdf生成PDF,在某些情况下,通过PHP和Mac上的一些shell调用,使用PDFTK合并生成的文件。

然而,我希望这些文档默认情况下由用户PDF阅读器在"单页视图"/"适合窗口"中打开。

我遇到了以下问题,这表明该功能已被列入PDFTk的功能请求列表,但我找不到任何关于它实现的参考。我也遇到过高级PDF工具(请参阅-q[OpenAction]标志)然而,这个工具似乎只适用于Windows,我需要一些支持OSX 的东西

您可以使用CPDF:来完成此操作

cpdf in.pdf -set-page-layout SinglePage AND -fit-window -o out.pdf

由于被询问者提到了命令行,这个答案假设读者知道如何创建脚本并使其可执行。

使用您的操作系统的包系统(对于MacOS,我建议brew),您应该能够安装python3 pike库。然后创建一个包含以下内容的Python脚本:

from pikepdf import Pdf, Dictionary, Name
pdf=Pdf.open('input.pdf')
pdf.root.PageLayout=Name('/SinglePage')
pdf.root.PageMode=Name('/FullScreen')
pdf.save('output.pdf')

当你运行它时,它会创建output.pdf,它是input.pdf的副本,只是pdf默认为单页布局和全屏。

您可以使用特殊的pdfmark运算符使用PostScript代码段,在Ghostscript的帮助下将适当的DOCVIEW设置插入到目标PDF中。

下面是一个嵌入在文本文件my-pdf-docview-pdfmark.ps:中的示例

[ /PageMode /UseOutlines  % Display bookmarks upon opening the doc
 %/PageMode /UseThumbs    % Display thumbnails upon opening the doc
 %/PageMode /FullScreen   % Open the document in fullscreen mode
 %/PageMode /None         % Display neither bookmarks nor thumbnails upon opening
  /Page 2                 % Open document with page 2, not page 1!
 %/View [ /XYZ null null null ]
                          % Go to specified page and retain same ... 
                          % ... horizontal/vertical offset+zoom as current page
  /View /Fit              % Fit page to window
 %/View /FitB             % Fit visible part of page to window
 %/View [/FitH 220]       % Fit page width to window; 220 is distance ...
                          % ... of page origin from top of window
                                         /DOCVIEW    pdfmark
 [ {Catalog} <<
              /PageLayout /SinglePage
             %/PageLayout /OneColumn
             %/PageLayout /TwoColumnRight
             %/PageLayout /TwoColumnLeft
             >>                          /PUT        pdfmark

注意,只看到左方括号[并不是错误,而没有看到右方括号。[是一个运算符,它由最终关键字pdfmark封闭。

上面的一些行未使用初始%字符进行注释,以向您展示其他备选方案。

请注意,这可能不适用于所有用户或所有观众。这些设置只是对观众的提示和建议,观众可能尊重这些设置,也可能不尊重这些设置。此外,用户可以覆盖其查看器的配置,并告诉它始终忽略这些提示,而是按照用户指定的方式打开所有PDF。

创建上述文件后,将其应用于PDF:

gs -o output.pdf               '
   -sDEVICE=pdfwrite           '
     my-pdf-docview-pdfmark.ps '
   -f input.pdf