12

I think the expected view (Fig. 2) is necessary because I work in environments where are many printers. I know the thread How to add print link answer which opens a general print window without the ability to specify fine details such as which pages to print by activation of the button. Reproduction of the code here with the addition of the target of this thread - customized action of the print button

% https://tex.stackexchange.com/a/30534/13173
\documentclass{scrbook}
\usepackage{hyperref}
\usepackage{pdfpages}
\usepackage{lipsum}

\begin{document}
\Acrobatmenu{Print}{Print doc}
\lipsum

% TODO include a link here which will print out the included pdf from the document
\includepdf[page={1-3}]{leo.pdf} % choose any multipage pdf file here 

\end{document}

Aim

  1. to include Print button on page which will print specific pages of the document
  2. to include Print button which will print current page of the document.

Fig. 1 Output of AlexG's button action where you get two times a popup, Fig. 2 View when pressing GUI's Print button

enter image description here enter image description here

Expected output GUI: Wanted settings in the GUI (Fig. 2)

Testing AlexG's proposal

Output in Fig. 1.

Engines: pdfTeX, XeLaTeX, ...
OS: Debian 8.7
PDF viewer: Adobe Acrobat 9.x (acroread) installed as described here

1 Answers1

14

This requires JavaScript and perhaps A-Reader. The buttons themselves are not printed. All engines.

Basic example using print() method with parameters: No print dialog possible when specifying page ranges; multiple calls of print() method needed for multiple page ranges:

\documentclass{article}

\usepackage{media9} % non-printable push buttons with JS actions
\usepackage{pgffor}

\begin{document}

Page \thepage

% print two page ranges
% Note: nStart/nEnd are zero-based
\mediabutton[
  jsaction={
    this.print({
      bUI: false, %no print dialog; mandatory if page range is specified
      nStart: 0,
      nEnd: 3,
    });
    this.print({
      bUI: false,
      nStart: 6,
      nEnd: 8,
    });
  }
]{\fbox{Print pp. 1--4 and 7--9}}

\foreach \i in {2,...,10} {
  \newpage
  Page \thepage

  % print current page
  \mediabutton[
    jsaction={
      this.print({
        bUI: false,
        nStart: this.pageNum,
        nEnd: this.pageNum,
      })
    }
  ]{\fbox{Print current page}}
}

\end{document}

Example with advanced settings using PrintParams object (only recent AR versions on Windows and OSX): Now, settings (target printer and other parameters) can be modified in the print dialog.

Properties of the PrintParams object are documented in the JavaScript for Acrobat API Reference.

\documentclass{article}

\usepackage{media9}
\usepackage{pgffor}

\begin{document}

Page \thepage

%print two page ranges, show dialog
\mediabutton[
  jsaction={
    var pp = this.getPrintParams();
    pp.interactive = pp.constants.interactionLevel.full; %show print dialog
    %pp.interactive = pp.constants.interactionLevel.automatic; %suppress dialog, but progress monitor and "cancel" shown
    %pp.interactive = pp.constants.interactionLevel.silent; %suppress everything
    pp.printRange=[[0, 3],[6, 8]]; %two ranges, again 0-based page nums
    this.print(pp);
  }
]{\fbox{Print pp. 1--4 and 7--9}}

\foreach \i in {2,...,10} {
  \newpage
  Page \thepage

  % (silently) print current page
  \mediabutton[
    jsaction={
      var pp = this.getPrintParams();
      pp.interactive = pp.constants.interactionLevel.silent;
      pp.firstPage=this.pageNum;
      pp.lastPage=this.pageNum;
      this.print(pp);
    }
  ]{\fbox{Print current page}}
}

\end{document}

Many of the settings in the print dialog can be pre-configured via the PrintParams object properties.

AlexG
  • 54,894
  • 2
    I have Cups installed to manage printers on my Linux box. The default printer is always chosen with my proposed code which works well. In order to be able to choose a specific printer for the page-range print buttons, some more coding would be necessary. The user could e. g. input the printer name in a text field of a custom dialog box. Unfortunately, there is no possibility to query available printers using JavaScript that could then be presented in a drop-down box. – AlexG Jun 02 '17 at 07:12
  • 2
    As noted in the code, if a page range/specific page is passed to the print() method, a print dialog cannot be shown; the bUI property must be set to false (according to the API Reference) which suppresses the dialog. Otherwise (bUI: true) the print button is not functional. This is how Acrobat handles printing and cannot be changed. (The logic behind that not clear to me but it is the way it is.) – AlexG Jun 02 '17 at 08:21
  • 1
    Do you mean the 2nd code box? It is only working correctly in Windows & OSX versions. I successfully tested in AR-XI on Windows. On Linux (AR-9.5.5) the print dialog pops up, but pre-set page ranges are not included. – AlexG Jun 02 '17 at 10:05
  • 1
    I'm afraid that's all I can do. The basic method (1st code) doesn't allow us to pass a printer name to the print() method and the configuration via PrintParams object is not functional on Linux. – AlexG Jun 02 '17 at 10:17
  • I extended the Linux part of the thread here https://unix.stackexchange.com/q/368836/16920 I will let you if we manage to figure out anything for the Linux support. – Léo Léopold Hertz 준영 Jun 02 '17 at 16:18