Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 684
Richard Avatar asked Aug 28 '14 08:08

Richard


2 Answers

You can do this with CPDF:

cpdf in.pdf -set-page-layout SinglePage AND -fit-window -o out.pdf
like image 96
johnwhitington Avatar answered Nov 14 '22 15:11

johnwhitington


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.

like image 2
hackerb9 Avatar answered Nov 14 '22 15:11

hackerb9