Reputation: 450
Short Question : Is there a PDF command line/shell tool (for osx) which can set the necessary property(ies) on a PDF for it to be opened initially in "Single Page View" (fit to window)
More Info
I'm currently generating PDFs using WkhtmlToPdf and in some cases merging the generated files using PDFTK via PHP and some shell calls on a Mac.
However, I'd like for these documents to be opened by the users PDF reader by default in "Single Page View" / "Fit to Window".
I've come across the following question which suggests this feature was put on the feature request list for PDFTk but I can not find reference to it ever being implemented. I've also come across Advanced PDF Tools (see the -q [OpenAction] flag) however, this tool appears to only be available for Windows and I need something which supports OSX
Upvotes: 6
Views: 1495
Reputation: 1952
Since the questioned mentioned the command line, this answer presumes the reader knows how to create scripts and make them executable.
Using your OS's package system (for MacOS, I suggest brew
) you should be able to install the python3-pike library. Then create a Python script that contains the following:
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')
When you run it, it will create output.pdf, which is a duplicate of input.pdf except the PDF defaults to a Single Page layout and fullscreen.
Upvotes: 2
Reputation: 90295
You can use PostScript snippets using the special pdfmark
operator to insert the appropriate DOCVIEW
settings with the help of Ghostscript into a target PDF.
Here is an example embedded in a text file, 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
Note, it is not an error to only see the opening square bracket [
, but no a closing one. [
is an operator, and it is closed by the final keyword pdfmark
.
Some of the lines above are un-commented with the initial %
character to show you other alternatives.
Be aware that this may not work for all users or for all viewers. These settings are only hints and recommendations to the viewers, who may or may not respect them. Also, users can override the configuration of their viewer and tell it to always ignore these hints and instead open all PDFs as the user specifies.
Once you created the above file, apply it to the PDF:
gs -o output.pdf \
-sDEVICE=pdfwrite \
my-pdf-docview-pdfmark.ps \
-f input.pdf
Upvotes: 1
Reputation: 2783
You can do this with CPDF:
cpdf in.pdf -set-page-layout SinglePage AND -fit-window -o out.pdf
Upvotes: 4